JavaScript Algorithms and Data Structures Projects - Cash Register

My code doesnt work. What am I doing wrong?

Your code so far

function checkCashRegister(price, cash, cid) {
  const currencyValues = {
    "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 change = [];

  // Calculate total cash in drawer
  let totalInDrawer = cid.reduce((total, curr) => total + curr[1], 0);

  // Check if drawer has enough cash
  if (totalInDrawer < changeDue) {
    return {status: "INSUFFICIENT_FUNDS", change: []};
  }

  // Check if drawer has exact change
  if (totalInDrawer === changeDue) {
    return {status: "CLOSED", change: cid};
  }

  // Iterate over currency denominations in reverse order
  for (let i = cid.length - 1; i >= 0; i--) {
    let denomination = cid[i][0];
    let denominationValue = currencyValues[denomination];
    let denominationCount = Math.floor(cid[i][1] / denominationValue);
    let amountToReturn = 0;

    // Calculate how much of this denomination to return
    while (changeDue >= denominationValue && denominationCount > 0) {
      changeDue = parseFloat((changeDue - denominationValue).toFixed(2));
      amountToReturn += denominationValue;
      denominationCount--;
    }

    // Add denomination to change array if it was used
    if (amountToReturn > 0) {
      change.push([denomination, amountToReturn]);
    }
  }

  // Check if change could not be made with exact denominations
  if (changeDue > 0) {
    return {status: "INSUFFICIENT_FUNDS", change: []};
  }

  // Return the change array
  return {status: "OPEN", change: change};
}


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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

Can you provide some more details about what’s happening?

Your code is passing for me.

Copy it and reset the challenge, then paste the code back in.

If that doesn’t work try it in a different browser.

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