JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:

i have tried everything, unable to pass these test cases. Please help me

Your code so far

function checkCashRegister(price, cash, cid) {
  const currencyUnits = [
    ["PENNY", 0.01],
    ["NICKEL", 0.05],
    ["DIME", 0.1],
    ["QUARTER", 0.25],
    ["ONE", 1.0],
    ["FIVE", 5.0],
    ["TEN", 10.0],
    ["TWENTY", 20.0],
    ["ONE HUNDRED", 100.0]
  ];

  let changeDue = cash - price;
  let totalCid = cid.reduce((sum, [_, amount]) => sum + amount, 0);
  
  // If total cash in drawer is less than the change due, or exact change cannot be given
  if (totalCid < changeDue) {
    return { status: "INSUFFICIENT_FUNDS", change: [] };
  }

  // If total cash in drawer is equal to the change due
  if (totalCid === changeDue) {
    return { status: "CLOSED", change: cid };
  }

  cid = cid.reverse();  // Reverse to start giving change from highest to lowest denomination
  let changeArray = [];

  for (let i = 0; i < currencyUnits.length; i++) {
    const [currency, currencyValue] = currencyUnits[i];
    let amountInDrawer = cid[i][1];
    let amountToReturn = 0;

    while (changeDue >= currencyValue && amountInDrawer > 0) {
      changeDue -= currencyValue;
      changeDue = Math.round(changeDue * 100) / 100;  // To handle floating point precision issues
      amountInDrawer -= currencyValue;
      amountToReturn += currencyValue;
    }

    if (amountToReturn > 0) {
      changeArray.push([currency, amountToReturn]);
    }
  }

  // If exact change cannot be given
  if (changeDue > 0) {
    return { status: "INSUFFICIENT_FUNDS", change: [] };
  }

  return { status: "OPEN", change: changeArray };
}

Your browser information:

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

Challenge Information:

JavaScript Algorithms and Data Structures Projects - Cash Register

Hello,
If you try to call the function with the arguments provided in the failed tests, you will see where your code is failing
For the first one

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]]))
/*
{status: "OPEN", change: [["QUARTER", 0.5]]}
*/

// second test
console.log(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]]))
/*
{
  "status": "OPEN",
  "change": [
    [
      "PENNY",
      96.74000000001259
    ]
  ]
}
*/

Notice how in both cases, the result is different from what the test expects, the returned values are not assigned to their respective currencies, they are instead combined into one value under one currency + the currency is wrong the first test
Make sure to change the code responsible for the creation of the changeArray variable to fix this

I’m still not able to figure out

function checkCashRegister(price, cash, cid) {
    const currencyUnits = [
        ["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 totalCid = cid.reduce((sum, curr) => sum + curr[1], 0).toFixed(2);

    if (totalCid < changeDue) {
        return { status: "INSUFFICIENT_FUNDS", change: [] };
    } else if (totalCid == changeDue) {
        return { status: "CLOSED", change: cid };
    } else {
        let changeArray = [];
        cid = cid.reverse();

        for (let i = 0; i < cid.length; i++) {
            let currency = cid[i][0];
            let currencyTotal = cid[i][1];
            let currencyValue = currencyUnits.find(unit => unit[0] === currency)[1];
            let amountToReturn = 0;

            while (changeDue >= currencyValue && currencyTotal > 0) {
                changeDue -= currencyValue;
                changeDue = changeDue.toFixed(2); // Avoid floating-point precision issues
                currencyTotal -= currencyValue;
                amountToReturn += currencyValue;
            }

            if (amountToReturn > 0) {
                changeArray.push([currency, amountToReturn]);
            }
        }

        if (changeDue > 0) {
            return { status: "INSUFFICIENT_FUNDS", change: [] };
        }

        return { status: "OPEN", change: changeArray };
    }
}

this is one of the help that I took as a lesson, there I could pass the test cases, but here I’ve been stuck for quite a while

have you checked what the failing tests return?

for example, this fail:

  1. 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]]) should return {status: "OPEN", change: [["QUARTER", 0.5]]} .

So I add

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

the output is

{ status: 'OPEN', change: [ [ 'PENNY', 0.5000000000000002 ] ] }

instead of doing this all in one line, you should convert everything to integers before any calculations, and divide at the last possible moment before returning

1 Like