I can't understand Cash Register Errors

Here’s my code for the completing the curriculum:



let penny = {
  number: 0,
  spent: 0,
  value: 1,
  name: "PENNY"
}

let nickel = {
  number: 0,
  spent: 0,
  value: 5,
  name: "NICKEL"
}

let dime = {
  number: 0,
  spent: 0,
  value: 10,
  name: "DIME"
}

let quarter = {
  number: 0,
  spent: 0,
  value: 25,
  name: "QUARTER"
}

let one = {
  number: 0,
  spent: 0,
  value: 100,
  name: "ONE"
}

let five = {
  number: 0,
  spent: 0,
  value: 500,
  name: "FIVE"
}

let ten = {
  number: 0,
  spent: 0,
  value: 1000,
  name: "TEN"
}

let twenty = {
  number: 0,
  spent: 0,
  value: 2000,
  name: "TWENTY"
}

let hundred = {
  number: 0,
  spent: 0,
  value: 10000,
  name: "ONE HUNDRED"
}

const callEachName = [penny, nickel, dime, quarter, one, five, ten, twenty, hundred];


function checkCashRegister(price, cash, cid) {
  let changeTotal = Math.round(cash * 100) - Math.round(price * 100);
  let change = {
    status: null,
    change: []
  }
  let allDenomToZero = 0;
  takeNumberOfCoins(cid);

  let totalMoneyInDrawer = 0;

  for (var y = 0; y < callEachName.length; y++){
    totalMoneyInDrawer += callEachName[y].number * callEachName[y].value
  }
 
  
  while (changeTotal > 0){
    if (changeTotal >= hundred.value && hundred.number > 0){
      changeTotal -= hundred.value; 
      hundred.number--;
      hundred.spent++;
    }
    else if (changeTotal >= twenty.value && twenty.number > 0){
      changeTotal -= twenty.value; 
      twenty.number--;
      twenty.spent++;
    }
    else if (changeTotal >= ten.value && ten.number > 0){
      changeTotal -= ten.value; 
      ten.number--;
      ten.spent++;
    }
    else if (changeTotal >= five.value && five.number > 0){
      changeTotal -= five.value; 
      five.number--;
      five.spent++;
    }
    else if (changeTotal >= one.value && one.number > 0){
      changeTotal -= one.value; 
      one.number--;
      one.spent++;
    }
    else if (changeTotal >= quarter.value && quarter.number > 0){
      changeTotal -= quarter.value; 
      quarter.number--;
      quarter.spent++;
    }
    else if (changeTotal >= dime.value && dime.number > 0){
      changeTotal -= dime.value; 
      dime.number--;
      dime.spent++;
    }
    else if (changeTotal >= nickel.value && nickel.number > 0){
      changeTotal -= nickel.value; 
      nickel.number--;
      nickel.spent++;
    }
    else if (changeTotal >= penny.value && penny.number > 0) {
      changeTotal -= penny.value; 
      penny.number--;
      penny.spent++;
    }  
    else {
      break;
    }
  }
  

  if (changeTotal>totalMoneyInDrawer || changeTotal != 0){
    change.status ="INSUFFICIENT_FUNDS";
    return change;
  }


  for (var x = 0; x < callEachName.length; x ++){
    allDenomToZero += callEachName[x].number
  }

  for (var n = callEachName.length; n>0; n--){
    if(callEachName[n-1].spent >= 1) change.change.push([
        callEachName[n-1].name, 
        (callEachName[n-1].spent * (callEachName[n-1].value/100))
       ])
  }

  if (allDenomToZero == 0) {
    change.status = "CLOSED";
    for (var f = 0; f<callEachName.length; f++){
    if(callEachName[f].spent == 0) change.change.push([
        callEachName[f].name, 
        (callEachName[f].spent * (callEachName[f].value/100))
       ])
    }
  }
  else change.status = "OPEN";

  

  console.log(change);
  return change;
  
  
}

function takeNumberOfCoins(cid){
  for (var x = 0; x < cid.length; x++){
    switch(cid[x][0]){
      case "PENNY":
        penny.number = Math.round(cid[x][1]/0.01);
        break;
      case "NICKEL":
        nickel.number = Math.round(cid[x][1]/0.05);
        break;
      case "DIME":
        dime.number = Math.round(cid[x][1]/0.1);
        break;
      case "QUARTER":
        quarter.number = Math.round(cid[x][1]/0.25);
        break;
      case "ONE":
        one.number = Math.round(cid[x][1]/1);
        break;
      case "FIVE":
        five.number = Math.round(cid[x][1]/ 5);
        break;
      case "TEN":
        ten.number = Math.round(cid[x][1]/10);
        break;
      case "TWENTY":
        twenty.number = Math.round(cid[x][1]/20);
        break;
      case "ONE HUNDRED":
        hundred.number = Math.round(cid[x][1]/100);
        break;
    }
  }
}

checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])

GIST: Unfortunately even if I did get the correct answers I can’t seem to advance. Are there problems with my code? I checked it multiple times but I can’t seem to find the problem.

Remove the function call at the end. Most likely you are mutating values outside the function (what are not getting reset between runs).

1 Like

Oh my gosh man thank you very much, I can’t believe a simple mishap like that could case such an effect. Again, thank you man! Have a great day!