Cash Register - confused for next step

Tell us what’s happening:
Describe your issue in detail here.

Been working at this for a while now. Got to the point where I feel like I’ve confused myself.

What I’m trying to achieve in the else statement is looping through my objectCurrency array and subtracting that value as long as the changeDue is greater than it. From there, its respective value from the CID array is also updated to reflect the change.

For example, if I need to return $65 and I have $60 worth of 20 dollar notes I would subtract 20 from the changeDue 3 times and the value in the CID array would subtract by twenty on each iteration.

60 → 40-> 20-> etc.

Then, when the CID value is zero, it goes to the next iteration as long as the change due is greater than it.

Obviously, translating my thoughts into code isn’t as effective as I thought and my code doesn’t work.

Any help or guidance would be appreciated.

   **Your code so far**

function checkCashRegister(price, cash, cid) {

    const currencyObject = [
    {currency: "penny", amount: 1},
    {currency: "nickel", amount: 5},
    {currency: "dime", amount: 10},
    {currency: "quarter", amount: 25},
    {currency: "one", amount: 100},
    {currency: "five", amount: 500},
    {currency: "ten", amount: 1000},
    {currency: "twenty", amount: 2000},
    {currency: "hundred", amount: 10000}
 ]

let changeDue = (cash * 100) - (price * 100);

let cidTotal = 0

let returnArray = [];

for (let variable of cid) {
  cidTotal += variable[1] * 100
}

if (cidTotal < changeDue) {
  return {status: "INSUFFICIENT_FUNDS", change: []}
}

else if (cidTotal == changeDue) {
return {status: "CLOSED", change: [...cid]}
}

else {

cid = cid.reverse();

for (let elem of cid) {
  elem[1] = elem[1] * 100
}


for (let i = currencyObject.length - 1; i >= 0; i--) {
  while (changeDue >= currencyObject[i].amount && cid[i][1]) {
     console.log(changeDue)
     changeDue -= currencyObject[i].amount;
     cid[i][1] = cid[i][1] - currencyObject[i].amount;
     returnArray.push(cid[i]);
  }
}

return {status: 'OPEN', change: returnArray}

}

}


checkCashRegister(2.5, 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 browser information:**

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

Challenge: Cash Register

Link to the challenge:

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