Cash Register project not passing tests

Hi, I am trying to do the cash register project but for some reason my code is not passing the tests. Even the first test is failing, and the first test is supposed to check whether you are returning an object. I have tried loggin typeof() and it logs as object. When I run the freecodecamp solution code on my browser it returns the exact same thing as my own code. Does anyone have any idea of what the problem might be?

I have tried to substitute my object “equivalences” for the exact same array of objects that it is used in the freecodecamp solution to see if that would help.
I have tried to declare the object that I am returning in the same fashion as the freecodecamp solution.
So far nothing seems to be working. Does anyone have any idea of why this might be?

Here is my code:


function changecalc(amount){
amount =  amount *100;
let breakdown =  [];
let equivalences =  {
  "PENNY": 1,
  "NICKEL": 5,
  "DIME": 10,
  "QUARTER": 25,
  "ONE": 100,
  "FIVE": 500,
  "TEN": 1000,
  "TWENTY": 2000,
  "ONE HUNDRED": 10000};
  Object.values(equivalences).reverse().forEach(val=>{
    i= 0;
    while (Math.round(amount - val) >=  0 && amount > 0) {
      amount =  Math.round(amount - val);
      i++;
    };
    if (i >0)  breakdown.push([Object.keys(equivalences).find(key => equivalences[key] === val) , (val/100)*i]);
  });
return breakdown;
};

function fundCalc (total, cash, price){
if ((total-(cash-price)) > 0) return 1;
if ((total-(cash-price)) < 0) return 2;
if ((total-(cash-price)) == 0) return 3;
};

function checkCashRegister(price, cash, cid) {
let change;
let total = 0;
let shrapnel =  {};
cid.forEach((value)=> total =  total + value[1]);
let newcid =  fundCalc(total, cash, price);
switch (newcid) {
  case 1:
    let returnedcash =  changecalc(cash-price);
    if (returnedcash.lenth ===  0) shrapnel = {status: "INSUFFICIENT_FUNDS", change: []};
    else  shrapnel = {status: "OPEN", change: changecalc(cash-price)};
    return shrapnel;
  case 2:
    shrapnel = {status: "INSUFFICIENT_FUNDS", change: []};
    return shrapnel;
  case 3:
    shrapnel = {status: "CLOSED", change: changecalc(cash-price)};
    return shrapnel;
}
}

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/78.0.3904.70 Safari/537.36.

Challenge: Cash Register

Link to the challenge:

Quick couple notes to get you moving forward.

  1. let i= 0; line 15 (if you open the console on internet browser that is flagged and will solve your main issue)

  2. Case 1 of your switch; returnedcash.length

1 Like

Thank you very much! It did work! Unfortunately I can’t say it is the first time I have made that mistake. Fantastic catch!