Cash register can't pass 2 of 5

I’ve been struggling with this one and got 3/5, but can’t pass the two ones that are supposed to return ‘open’ with an array of the bills and coins you used to complete the change.
This is my code so far, the console.log() of the array is giving me everything perfect, but gives me 0.03 PENNYS instead of 0.04.
And in the other check i’m supposed to return {status: "OPEN", change: [["QUARTER", 0.5]]} but i am returning nothing.
plss help :C


function checkCashRegister(price, cash, cid) {
let change = cash - price;
let sum = 0;
let arr = [];
 let objcid = {};
 let objvalue = {
    'ONE HUNDRED': 100,
    TWENTY:	20,
    TEN: 10,
    FIVE:	5,
    ONE: 1,
    QUARTER: 0.25,
    DIME: 0.1,
    NICKEL: 0.05,
    PENNY: 0.01
 }

   cid.forEach((v) => {

      // Extract the key and the value
      let key = v[0];
      let value = v[1];

      // Add the key and value to the object
      objcid[key] = value;
  });


for (let prop in objcid)
{
sum += objcid[prop];
}

if (change === sum)
{ 
return {status: "CLOSED", change: cid};
}

else 
{
for (let prop in objvalue)
{
  let amount = 0
  while (objvalue[prop] <= change && objcid[prop] > 0)
  {
    amount += objvalue[prop];
    change -= objvalue[prop];
    objcid[prop] -= objvalue[prop];
    sum -= objvalue[prop];
  }
  if (change !== 0 && amount !== 0)
  {
    if (prop === 'PENNY')
    {
      amount += 0.01;
    }
    arr.push([prop, amount]);
  }
}

console.log(arr)

  if (change !== 0) 
  {
    return {status: "INSUFFICIENT_FUNDS", change: []};
  }
  else if (change === 0 && sum > 0)
  {
    return {status: "OPEN", change: arr}
  }
}
};

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]])
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:98.0) Gecko/20100101 Firefox/98.0

Challenge: Cash Register

Link to the challenge:

This is one of my favourite projects for this reason. What you are encountering is a floating point calculation error. This happens because computers run on binary language (0s and 1s) and struggle with accurately representing decimal numbers sometimes.

Try doing a console.log(0.1 + 0.2) and you can see exactly what’s going on.

There is thankfully a pretty neat way around this issue for the cash register project - because you are dealing with American denominations, everything will only be precise to a maximum of two decimal places.

What can you do to a number to turn two decimal places into zero decimal places? Multiply by 100 :grin:

2 Likes

Yes i found this yesterday, i was 4hs trying to solve this and it was a problem the computers have with float calculations. Thank you!

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