Cash Register Project. Run Tests button is not working

I’ve been trying to run my code since last night and the Run tests button just doesn’t respond.
I always code on Repl and then paste it in the FCC editor. I don’t know what the problem is.

My Code:

function checkCashRegister(price, cash, cid) {
  // Here is your change, ma'am.
  
 let changeToReturn =  cash - price;  
 let changeArray = [["PENNY", 0],["NICKEL", 0],["DIME", 0],
["QUARTER", 0],["ONE", 0],["FIVE", 0],["TEN", 00],["TWENTY", 0],["ONE HUNDRED", 0]];
 let copyCid = cid.slice();
  
 let cashAvailable = 0;
  for(let i=0; i<copyCid.length; i++){
      cashAvailable += copyCid[i][1];
  }
 let sumChange = 0, changeAvailable;
  let j = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
  //console.log(changeToReturn);
  for(let i=copyCid.length-1; i>=0; i--){
    sumChange = j[i];
    
    //console.log("-----"+sumChange);
    while(changeToReturn/j[i]>=1 && copyCid[i][1]>=j[i]){
      changeToReturn -= j[i];
      changeToReturn = (changeToReturn).toFixed(2);
      copyCid[i][1] -= j[i]; 
      copyCid[i][1] = copyCid[i][1].toFixed(2);
      changeArray[i][1] += j[i];
     //changeArray[i][1] = parseFloat(changeArray[i][1]).toFixed(2);
      cashAvailable -= j[i];
      cashAvailable = cashAvailable.toFixed(2);
     // console.log(cashAvailable);
     // console.log(" = "+sumChange+"==="+j[i]+"==="+copyCid[i][1]+"=="+changeArray[i][1]);
      sumChange += j[i];
    }
    //console.log("*"+changeToReturn);
    if(changeToReturn<=0.0){
      changeAvailable = true;
      //console.log("Change successfully returned");
     // break;
    }
  }
  
  //arranging array from larger bills to smaller, 100 - 0.01
  let finalArray = [];
  for(let i=changeArray.length-1; i>=0; i--){
    finalArray.push(changeArray[i]);
  }

  if(changeAvailable){
    if(cashAvailable <= 0.01){
      return {
        status: "CLOSED",
        change: finalArray
      };
    }
    else{
      return{
        status: "OPEN",
        change: finalArray.filter(val => val[1]>0)
      };
    }
  }
  else{
    return{
      status: "INSUFFICIENT_FUNDS",
      change: []
    };
  }
  return cashAvailable;
}

My code is not perfect yet, some minor changes are needed, but at least the test should show tests failed error, it just doesn’t seem to bother with the click. I’ve checked for infinite loops, the code works smoothly. You can doublecheck it here https://repl.it/@VineshSwargam/CashRegister

One other problem apart from Run Tests button is that, in the code link I gave, if you see there, I used the slice method to copy the cid array into another array to perform operations on the copied array so as to not mutate the original array. But, after the code runs past a for loop( one with the while loop in it), the cid array, which is not even present in any calculations, is mutated in the last iteration of the for loop. I’ve been trying to understand what’s going on, but nothing seems wrong to me.
Any help is much appreciated.
Thank you!

Project Link: https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/

I didn’t check your code but i wanted to suggest to check for syntax errors because in my experience the run button usually seems to not work when it finds any syntax issue. (Does your external editor check javascript syntax?)

Yes, it does, and it is very strict about it.
So, am sure there are no syntax errors.
I’ve been trying to make run it, so far what I’ve found is, if I remove the changeArray I created, it runs the tests and obviously fails all because I need that array.
I don’t know what the problem is with that array.

Is TEN supposed to be two zeros in the array? Can you try with one zero? Sorry if this is a stupid guess…

Haha. If there’s anything I’ve learnt in my short tenure as a programmer, it is that all that can be tried, must be tried. Nothing is stupid, nothing is out of bounds.
However, no the zeroes are not the problem.
I just pasted my code there without the changeArray and declared the changeArray in FCC editor. It works. Thank you, though.
I do need a little help with the second part of my question. :confused:

1 Like

Is it possible that your copy is too shallow? Since cid is an array of arrays perhaps you need to copy it on a deeper level…

Looks like i guessed correctly

I’ll read more on that.
One question, though. Can you visit the Repl link and tell me if my code is understandable?

Actually, even if the slice method is a problem, it should effect the returned array from the method, right? Not the array it is used on.
Here, the array cid, passed as the argument, seems to change itself. Even though it was never referenced anywhere. How’s that even possible?

because you are only making a ‘shallow’ copy of the given cid array.

i’m not sure if you know what memory pointers are. This is possibly an advanced topic…
For now try to make a complete copy of the cid array (use a for loop to do that) and that should prevent the original array from being touched.

Oh, so slicing a multidimensional array not only returns a shallow copy, but it also alters the original array?

It is hard to explain without explaining memory pointers to you…

think of a ‘shallow copy’ as just a copy to the uppermost array while still pointing to the original nested arrays…
the nested arrays never get copied with slice

here’s another link for you to read
http://prestonparry.com/articles/ShallowCopiesAndSlice/

Edit: you’re a computer science major right? Have you studied computer memory yet?

Thank you, I know now to be careful with the slice method. Although it bugged me that an array, untouched, altered it’s values at a specific point, it is not my prime concern right now.
In the project, I passed all test cases but one. I know why am failing it. The returning object has a property called change with a value of what denomination of currency we used to return the change. So, am returning the object with value of change as ["Penny: 0.500000000002], whereas it should be [Penny: 0.5].
At a point in the loop, the calculations go slighly bizarre. Like 0.1+0.2 must equal 0.3, works fine. After 6th loop, 0.5 doesn’t become 0.6, it becomes 0.60000005 or something.
I really hope you take a look at my code, it’s documented.
If you run it as it is, you’ll see what the problem is.
Thank you.

PS: Yes, am a CS major. We did have computer memory. However, colleges here are only for namesake. They don’t teach us anything useful. You’re supposed to pass on your own. Nobody even encourages coding. It is sad, but that’s just how it is.

Yes this issue is known as the floating point issue. If you search the forum you will see how others handled it. If you search google it will explain to you why floats are weird… ( at least when it comes to computers)

I found another ref for you to read. This one explains how arrays are structured in memory. This is the basic stuff to understand in order to understand what is shallow vs deep copy (and why your original array elements got changed)

Thank you!
This helped a lot. You always find references for me to read up and understand what’s wrong and what must it instead be. I really appreciate it. :kissing_heart:
I changed my approach to the project because I wasn’t able to deal with floats no matter what I tried.
So, I just multiplied all floats in the problem by 100 for all operations and divided them by 100 before output. It worked, only cus I did proper deep cloning.
Thank you, again.

yes some people on the forum do the same thing (multiply to make cents)
some people use rounding method (Math.round) with only 2 decimal accuracy.
i think you will probably hit this one again so a little research on different methods will help you in future. (it won’t always be dollars and cent problems that give you trouble)

I’m glad you found the last reference helpful.
Have you heard of the free online CS 50 course offered by Harvard University? I was thinking you might benefit from it at some point (just in case it covers things you haven’t learned yet. Most universities start with level 100 courses, so level 50 is really introductory and that’s why they offer it free. But still I heard good things about it).