Initial workings on Cash Register exercise

Hi all! I’m posting my initial thoughts here before tomorrow’s class. I know the code is very sketchy and unfinished, but I like to do this, so I can return to it after tomorrow’s class and improve on it.

Doing this also means I stop working on it, and can go back and try the Intermediate Algorithm Scripting homework and hopefully get that done in time for tomorrow.

function checkCashRegister(price, cash, cid) {
  // sort coins and bills in order
   cid.sort((num1, num2) => num1-num2);
  console.log(cid.sort((num1, num2) => num1-num2));
  let change = (cash - price);
  console.log(change);
  if (change < cid) {
  return {status: "INSUFFICIENT_FUNDS", change: [("change")]};
} else if (change === cid) {
  return {status: "CLOSED", change: [(cid)]};
} else
//for (let i = 0; i < cid.length; i++) {
//let changeDue = [];

//const changeDue = 
 return {status: "OPEN", change: [(change)]};

}
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]]));

Here are the contents of the console with the following code. If I have time before tomorrow I will try and work out why it is skipping to the last return statement and returning that as the default, but for now, it’s too complicated for me to think about any more :sweat_smile: :



[ [ 'PENNY', 1.01 ],
  [ 'NICKEL', 2.05 ],
  [ 'DIME', 3.1 ],
  [ 'QUARTER', 4.25 ],
  [ 'ONE', 90 ],
  [ 'FIVE', 55 ],
  [ 'TEN', 20 ],
  [ 'TWENTY', 60 ],
  [ 'ONE HUNDRED', 100 ] ]
0.5
{ status: 'OPEN', change: [ 0.5 ] }
2 Likes

I think it’s because cid isn’t a number, so it can’t compare with change. I made the same mistake. Still haven’t got it to work completely, though :sweat_smile:

2 Likes

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