Cash Register - pleas help with my code

In the last test show like i fail but wen i see, the console the answer is the same i dont understand what is failing HELP!

. sorry for my english


function checkCashRegister(price, cash, cid) {
 var dic=cid.reverse();
 var turn=[["PENNY", 0.01], ["NICKEL", 0.05], ["DIME", 0.1], ["QUARTER", 0.25], ["ONE", 1], ["FIVE", 5], ["TEN", 10], ["TWENTY", 20], ["ONE HUNDRED", 100]];
 var change = cash-price; // get change
 var temp=0.0;
 var money=0.0;
 var mult=0;
 var anschange=[];
 var totalcid = cid.map((a) => a[1]).reduce((a,b) => a+b,0); // get the total in cid
 if(change >totalcid){ 
   return {status: "INSUFFICIENT_FUNDS", change:[]};
 }
 console.log(price, cash, cid); //--only test
 if(change == totalcid){ // the problem?
 console.log({status: "CLOSED", change: cid}) //--only test
   return {status: "CLOSED", change: cid};
 }
 for(var i in dic){
   temp=Math.floor(change/turn.find((a)=>a[0]==dic[i][0])[1]); // get the number of coins we ned (specific value)
   money=dic[i][1]/turn.find((a)=>a[0]==dic[i][0])[1]; // get the number of coins we have (specific value)
   if(change > 0){
     if(money >= temp){
       mult = temp
   }else{
     mult = temp - money;
     mult = temp - mult;
   }
   if(mult*dic[i][1]>0){ 
     change-=mult*turn.find((a)=>a[0]==dic[i][0])[1];
     change=Math.round(change *100)/100; 
     anschange.push([dic[i][0],mult*turn.find((a)=>a[0]==dic[i][0])[1]]);
     
   }
   }
   if(change>0 && i == cid.length-1){
     return {status: "INSUFFICIENT_FUNDS", change:[]};
   }
 }
return {status: "OPEN", change: anschange};
}


checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36

Challenge: Cash Register

Link to the challenge:

var dic=cid.reverse();

This line is changing the cid array as reverse is a destructive method. You can still use reverse, but you’ll need to use it on a clone of cid rather than cid itself.

Create a new variable and equal it with CID replacing it in reverse () but that did not resolve the problem, what can I do?

You cannot just assign a new variable to cid as they would both point at the same array. You need to make a new array with the same values as cid. The spread operator is one method of making a new array with the same values, but there are multiple methods that return a new array with the same values depending on the arguments handed to them.

Thank you this resolves after creating a new Variable Concat CID to clone it “var cid2 = [ ] .concat (CID);” Thank you so much

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.