Cash register code not working

Tell us what’s happening:
Hey, kind people of freeCodeCamp,
I have made code for the Cash Register challenge.
However, I don’t understand why it’s broken.
Please don’t spoil the challenge, I just want debugging info.
BTW the console.logs used to be returns

Your code so far


function checkCashRegister(price, cash, cid) {
let moneyValues = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
let drawerValue = 0;//defining how much money in the drawers
let value = 0;
let arrReturn = [];
let openStatus = true;
for(var i = 0; i < cid.length; i++) {
    drawerValue = drawerValue + cid[i][1];//the value that is being returned
}
let changenum = cash - price;
if(drawerValue < changenum) {
  return({
    status: "INSUFFICIENT_FUNDS",
    change: []
  })
}
for(i = moneyValues.length; i > 0; i = i - 1) {
  value = 0;
  while(moneyValues[i] <= changenum && changenum > 0 && cid[i][1] > 0) {
    cid[i][1] = cid[i][1] - moneyValues[i];
    changenum = changenum - moneyValues[i];
    value = value + moneyValues[i];
     
  }
  if(value) {
      arrReturn.push([cid[i][0], value]);
    }
}
let drawerVal2 = 0; // this is used to see if the drawer is closed
 for(var i = 0; i < cid.length; i++) {
    drawerVal2 = drawerVal2 + cid[i][1];//the value that is being returned
}
if(drawerVal2 === 0) {
  openStatus = false;
}
let returnVal2 = 0; // this is used to see if the drawer is closed
 for(var i = 0; i < arrReturn.length; i++) {
    returnVal2 = returnVal2 + arrReturn[i][1];//the value that is being returned
}
if(returnVal2 === cash - price) {
if(openStatus === false) {
return({
  status: "CLOSED",
  change:arrReturn
});
} else {
console.log({
  status: "OPEN",
  change:arrReturn
})
}
} else {
console.log({
  status: "INSUFFICIENT_FUNDS",
  change:[]
})
}
}
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36.

Challenge: Cash Register

Link to the challenge:

Hello people! I still need help! I’m so sorry to be bothering you, but I’m really confused right now. This code defies all my debugging attempts.

return({
  status: "CLOSED",
  change:arrReturn
});
} else {
console.log({
  status: "OPEN",
  change:arrReturn
})
}
} else {
console.log({
  status: "INSUFFICIENT_FUNDS",
  change:[]

Okay, first thing is to change both of your console.log to return, or the function only returns undefined.

It says up there that the console.log used to be return. I was using these to debug.


When I console.log() the function and call an argument from a failing test, it looks like your check to see if the drawer has enough cash is not accurate.


Sticking some console.log in your loops, it looks like maybe your loops aren’t running when you want them to?

Oh thank you! I’ll check that out!