Cash register : while?

Hello !

I’ve got an issue :

I can’t find the solution for the ‘while’ loop…

My reasoning is:

  • Return the CID table, and see if the price difference fits well in the table.

  • For this, I compare from largest to smallest until I can give change…

But I don’t know how to do it at all… and I need help…

  **Your code so far**

function checkCashRegister(price, cash, cid) {
let totalAmount = 0;
let diffMoney = cash - price;

cid.forEach(element => {
  const [ unit, amount ] = [element[0], element[1]]
  totalAmount += amount

if(diffMoney > totalAmount ) {
  return {status: "INSUFFICIENT_FUNDS", change: []}
} else if (diffMoney === totalAmount) {
  return {status: "CLOSED", change: cid}
} else {
  cid = cid.reverse()
  for( let elem of cid ) {


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/97.0.4692.99 Safari/537.36

Challenge: Cash Register

Link to the challenge:

I think you need to find a way to check which is the highest value (e.g. a dime) that can be evenly divided by the diffMoney, (with a result that is not smaller than that value) and repeat till 0. Hope this helps

I see, i will try, thanks !

1 Like

I’m stuck, i don’t know what im doing…

I have no idea what to do with the ‘while’ loop…

I feel like I never understood anything in javascript and I’m stuck in front of my computer for hours…

Maybe try if/else statements to implement the checking of values. You could check whether diffMoney is bigger than the biggest value in your register (i.e. 100), find the difference, etc. Is this helpful?

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