Cash Register seems to be ok but cant pass

Tell us what’s happening:

Hello everyone,

I’m just finished the code but the third test seems to be wrong, when i compare the expected result with my output i see the same and i don’t know why the test doesn’t success.

My output vs expected result
Capture

I appreciate if you can help me.

Your code so far


let UNITS = [{name: "PENNY", value: 0.01},
{name: "NICKEL", value: 0.05},
{name: "DIME", value: 0.1},
{name: "QUARTER", value: 0.25},
{name: "ONE", value: 1},
{name: "FIVE", value: 5},
{name: "TEN", value: 10},
{name: "TWENTY", value: 20},
{name: "ONE HUNDRED", value: 100}];

function checkCashRegister(price, cash, cid) {
  let change = cash - price;
  let arrChange = [];
  let newCid = [];
  cid.forEach(x => newCid.push([...x]));  
  UNITS.reverse().forEach((unit, i) => {
    if(unit.value < change && change > 0){
      let res = extract(change, unit, newCid);
      change = res.amount;
      newCid = res.cid;
      arrChange.push(res.change);
    }
  });
  if(change === 0){
    if(newCid.every(item => item[1] === 0)){
      return {
        status: "CLOSED",
        change: cid
      }
    }else{
      return {
        status: "OPEN",
        change: arrChange.sort((a, b) => b[1] - a[1])
      }
    }
  }else{
    return {
      status: "INSUFFICIENT_FUNDS",
      change: []
    }
  }
}

function extract(amount, currency, cid){
  let max = cid.find(curr => curr[0] === currency.name);
  let change = 0; 
  while(amount - currency.value >= 0 && currency.value <= max[1]){
    amount = +(amount - currency.value).toFixed(12);
    max[1] = +(max[1] - currency.value).toFixed(12);
    change = +(change + currency.value).toFixed(12);
  }
  return {
    amount,
    cid,
    change: [currency.name, Number(change.toFixed(2))]
  }
}

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

Your browser information:

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

@sebas0126 I just finished this exercise so it’s still fresh in my mind. If you look at the requirements of what the output should be for the three cases you’ll start to see that this assignment has nuances that might be missed at first glance.

Compare your console log result for the test case mentioned in your screenshot with what it should return. You’ll notice that they are different .

Good luck with this one. It’s a doozy but harder problems give you a greater sense of accomplishment once you figure them out!

Also a note for asking for help: I’d avoid screenshots because it means you are going to get less helpful answers because I couldn’t copy and paste the test case into your console statement to show you the actual difference between what you are getting vs. what the assignment expects.