I'm stuck can someone help me!

Tell us what’s happening: for some reasons the code isn’t getting inside the for and while loops hence it returns the rest array unmodified (in the case of OPENED). i want to know why for while loops aren’t working like it should be

Your code so far


function checkCashRegister(price, cash, cid) {
  let rest = [ 
  [ 'ONE HUNDRED', 0 ],
  [ 'TWENTY', 0 ],
  [ 'TEN', 0 ],
  [ 'FIVE', 0 ],
  [ 'ONE', 0 ],
  [ 'QUARTER', 0 ],
  [ 'DIME', 0 ],
  [ 'NICKEL', 0 ],
  [ 'PENNY', 0 ] 
  ];
  var curr = [ 
  [ 'ONE HUNDRED', 100 ],
  [ 'TWENTY', 20 ],
  [ 'TEN', 10 ],
  [ 'FIVE', 5 ],
  [ 'ONE', 1 ],
  [ 'QUARTER', 0.25 ],
  [ 'DIME', 0.1 ],
  [ 'NICKEL', 0.05 ],
  [ 'PENNY', 0.01 ] 
  ];
  var back = {status: "", change: []}
  let change_total = cash - price;
  
  let cid_total = cid.reduce((total, cur) =>total + Math.round( cur[1]*100)/100,0);
  
  console.log(cid_total);
  
  if(cid_total < change_total){
    return( {status: "INSUFFICIENT_FUNDS", change : back.change});
  }
  if(cid_total === change_total){
    return {status: "CLOSED", change: cid}
  }
  console.log(curr[0][1]);
  for(let i = 0; i < curr.length; i++){
    while(change_total - curr[i][1] > 0){
      
      rest[i][1] = rest[i][1] + curr[i][1];
      change_total = change_total - curr[i][1];
    }
  }
  if(cid_total > change_total){
    return {status: "OPEN", change: rest};
  }
}

console.log(checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]));

Challenge: Cash Register

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register

try to add console.log({i, change_total, "curr[i][1]": curr[i][1], "change_total % curr[i][1]": change_total % curr[i][1]}) as first line inside your for loop to check if everything has the value you expect it to be

1 Like

Didn’t you mean !== 0?

no i mean == 0

okay i will try that now

Can i ask you a question please ? it says :
checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]) should return {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]} .
but my code is returning : { status: ‘OPEN’,
change:
[ [ ‘ONE HUNDRED’, 0 ],
[ ‘TWENTY’, 80 ],
[ ‘TEN’, 10 ],
[ ‘FIVE’, 5 ],
[ ‘ONE’, 1 ],
[ ‘QUARTER’, 0.5 ],
[ ‘DIME’, 0.2 ],
[ ‘NICKEL’, 0 ],
[ ‘PENNY’, 0.03 ] ] }

I don’t understand why TWENY should get 60 and not 8O intead and why TEN gets 20 and not 10 like mine ! can you please help me i’m so confused ?

function checkCashRegister(price, cash, cid) {
  let rest = [ 
  [ 'ONE HUNDRED', 0 ],
  [ 'TWENTY', 0 ],
  [ 'TEN', 0 ],
  [ 'FIVE', 0 ],
  [ 'ONE', 0 ],
  [ 'QUARTER', 0 ],
  [ 'DIME', 0 ],
  [ 'NICKEL', 0 ],
  [ 'PENNY', 0 ] 
  ];
  var curr = [ 
  [ 'ONE HUNDRED', 100 ],
  [ 'TWENTY', 20 ],
  [ 'TEN', 10 ],
  [ 'FIVE', 5 ],
  [ 'ONE', 1 ],
  [ 'QUARTER', 0.25 ],
  [ 'DIME', 0.1 ],
  [ 'NICKEL', 0.05 ],
  [ 'PENNY', 0.01 ] 
  ];
  var back = {status: "", change: []}
  let change_total = cash - price;
  
  let cid_total = cid.reduce((total, cur) =>total + Math.round( cur[1]*100)/100,0);
  
  console.log(cid_total);
  
  if(cid_total < change_total){
    return( {status: "INSUFFICIENT_FUNDS", change : back.change});
  }
  if(cid_total === change_total){
    return {status: "CLOSED", change: cid}
  }
  console.log(curr[0][1]);
  for(let i = 0; i < curr.length; i++){
    while(change_total - curr[i][1] > 0){
      
      rest[i][1] = rest[i][1] + curr[i][1];
      change_total = change_total - curr[i][1];
    }
  }
  if(cid_total > change_total){
    return {status: "OPEN", change: rest};
  }
}

console.log(checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]));


look at the input, there are only 60$ in the 20$ denomination, so you can’t give 80$ using that, as there are not enough pieces there

1 Like

thank you ! i tried to fix a bit now it’s returning everything right exept the PENNY it’s giving 0.03 instead 0.04 not sure why !
in this case : 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]]) should return {status: "OPEN", change: [["QUARTER", 0.5]]} .

function checkCashRegister(price, cash, cid) {

  let arr = cid.reverse();

  let rest = [ 

  [ 'ONE HUNDRED', 0 ],

  [ 'TWENTY', 0 ],

  [ 'TEN', 0 ],

  [ 'FIVE', 0 ],

  [ 'ONE', 0 ],

  [ 'QUARTER', 0 ],

  [ 'DIME', 0 ],

  [ 'NICKEL', 0 ],

  [ 'PENNY', 0 ] 

  ];

  var curr = [ 

  [ 'ONE HUNDRED', 100 ],

  [ 'TWENTY', 20 ],

  [ 'TEN', 10 ],

  [ 'FIVE', 5 ],

  [ 'ONE', 1 ],

  [ 'QUARTER', 0.25 ],

  [ 'DIME', 0.1 ],

  [ 'NICKEL', 0.05 ],

  [ 'PENNY', 0.01 ] 

  ];

  var back = {status: "", change: []}

  let change_total = cash - price;

  

  let cid_total = cid.reduce((total, cur) =>total + Math.round( cur[1]*100)/100,0);

  

  console.log(cid_total);

  

  if(cid_total < change_total){

    return( {status: "INSUFFICIENT_FUNDS", change : back.change});

  }

  if(cid_total === change_total){

    return {status: "CLOSED", change: cid}

  }

  console.log(curr[0][1]);

  for(let i = 0; i < curr.length; i++){

    let j = cid[i][1]/curr[i][1];

    while(change_total - curr[i][1] > 0 && j > 0){   

      rest[i][1] = rest[i][1] + curr[i][1];

      change_total = change_total - curr[i][1];

      j--;

    }

  }

  for(let i = 0; i < rest.length; i++){

    if(rest[i][1] == 0){

      rest.splice(i,1);

    }

  }

  if(cid_total > change_total){

    return {status: "OPEN", change: rest};

  }

}

console.log(checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]));

your change_total ends to be something like 0.009999999999994869
you have met floating point math
your best bet is to change everything to whole numbers at the beginnging (so you are counting the pennies), and then change back everything to dollars before returning
it is how it is done when dealing with money at least

2 Likes

how do i change it to a whole number ? isn’t it already a number ? and how do i change something to dollar sorry i’m asking so many questions

the way financial institutions do is to bring everything to pennies and do the calculations in pennies

so if we have 1.22$ and 4.01$ to sum, which results in 5.2299999999999995$
what do they do is make sure they deal only with whole numbers, so they change everything to pennies, so multiply everything by 100
so they have instead 122 pennies and 401 pennies, that summed make exactly 523 pennies
and then after they have finished all calculations and need to show the number, only then convert do dollars by dividing by 100: 523/100 gives 5.23, and voilà, exact result of 5.23$

You can also look at this for the explanation about floating point math:

1 Like

thank you so much i appreciate all the help !