JavaScript cash register

Hi everybody i have been working on the cashRegister challenge since two days now but i really still can’t figure out a logical solution. Need a little help of what i’m missing out

function checkCashRegister(price, cash, cid) {
  let currency=[
  ["PENNY", 0.01],
  ["NICKEL", 0.05],
  ["DIME", 0.1],
  ["QUARTER", 0.25],
  ["ONE", 1],
  ["FIVE", 5],
  ["TEN", 10],
  ["TWENTY", 20],
  ["ONE HUNDRED", 100]
];

  let changeDue=cash-price;
  let changeArr=[];
  let sum=0;
  cid.forEach(function(array){
    sum+=array[1];
  })

  if(sum<changeDue){
    return {status: "INSUFFICIENT_FUNDS", change: []}
  }
  else if(sum===changeDue){
    return {status: "CLOSED", change: cid}
  }
  return changeArr;
}

console.log(0.5%0.5)
console.log(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]]));
console.log(checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]));

Imagine you’re behind the register, the first thing you determine how much change is due, then you would start counting the change from the highest possible bill making your way to the smallest bill. Right now you’re counting total amount of money in register, which makes very little sense - let’s say you only have one $100 bill but have to give $1 change - that’s still INSUFFICIENT_FUNDS case, regardless of how much money you have in total

2 Likes

okay i understand that part of the problem but i’m stuck at the logic like how i should proceed sincerely i find this one really hard

I thought it was about the same logic as the Roman Numeral one.

1 Like

i feel like roman numeral was much easier compared to this one because i should apply condition to each currency unit too

Any complex task consists of number of simple tasks:

  1. Write a function that will take change amount and determine highest possible bill
  2. Choose correct data structure that will allow immediate lookup of the money available at that highset bill and switching to the next highest bill if amount not available

That’s pretty much it - the rest is just flow control between these two

1 Like

okay i’ll try to go on with your ideas thx