JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:

Describe your issue in detail here.

Your code so far

function checkCashRegister(price, cash, cid) {
  let change = cash - price;
  let totalCid = 0;
  for (let i = 0; i < cid.length; i++) {
    totalCid += cid[i][1];
  }
  totalCid = totalCid.toFixed(2);
  if (totalCid < change) {
    return { status: "INSUFFICIENT_FUNDS", change: [] };
  } else if (totalCid == change) {
    return { status: "CLOSED", change: cid };
  } else {
    let changeArr = [];
    const currencyUnit = [
      ["ONE HUNDRED", 100],
      ["TWENTY", 20],
      ["TEN", 10],
      ["FIVE", 5],
      ["ONE", 1],
      ["QUARTER", 0.25],
      ["DIME", 0.1],
      ["NICKEL", 0.05],
      ["PENNY", 0.01]
    ];
    for (let i = 0; i < currencyUnit.length; i++) {
      let currencyValue = 0;
      while (cid[i][1] > 0 && change >= currencyUnit[i][1]) {
        change -= currencyUnit[i][1];
        cid[i][1] -= currencyUnit[i][1];
        currencyValue += currencyUnit[i][1];
        change = change.toFixed(2);
      }
      if (currencyValue > 0) {
        changeArr.push([currencyUnit[i][0], currencyValue]);
      }
    }
    if (change > 0) {
      return { status: "INSUFFICIENT_FUNDS", change: [] };
    } else {
      return { status: "OPEN", change: changeArr };
    }
  }
}

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

//This return tonnes of error that I can not even debug. I understand that it is a test best kindly seek for help from those that have passed this stage or anyone that can. Thanks @all 

Your browser information:

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

Challenge Information:

JavaScript Algorithms and Data Structures Projects - Cash Register

What errors specifically? What did you try so far?

Apologies, I didn’t know I have to include the error message. Kindly find it below. Thanks @sanity

// running tests

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

. // tests completed // console output { status: ‘OPEN’, change: [ [ ‘QUARTER’, 0.5 ] ] }

It seems like you might be looking at the result of different test case, what would add the confusion.

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

Returns

{ status: 'OPEN',
  change: 
   [ [ 'TWENTY', 20 ],
     [ 'TEN', 10 ],
     [ 'FIVE', 5 ],
     [ 'ONE', 61 ],
     [ 'QUARTER', 0.5 ],
     [ 'DIME', 0.2 ],
     [ 'PENNY', 0.04 ] ] }

Your currencyUnit is going from largest to smallest but the cid is from smallest to largest. It should work if you reverse the cid before using it in the for/while loops.