Final cash register problem

Tell us what’s happening:

you may not think this is cleanest solution but i think this method should work, but it doesn’t. Could someone please give me a hint?

Your code so far


function checkCashRegister(price, cash, cid) {
  var change=cash-price;
  var hand=[], dstatus;
  console.log(change);
  var tcid=Math.round(cid[0][1]+cid[1][1]+cid[2][1]+cid[3][1]+cid[4][1]+cid[5][1]+cid[6][1]+cid[7][1]+cid[8][1]);
  if(change==tcid){
    dstatus="CLOSED";
  }else if(change>tcid){
    return {status: "INSUFFICIENT_FUNDS", change: []};
  }else{
    dstatus="OPEN";
  }

  if(change>=100&&cid[8][1]>=100){
    hand.push(["ONE HUNDRED", 100]);
    change=change-100;
  }
  if(change>=80&&cid[7][1]>=80){
    hand.push(["TWENTY", 80]);
    change=change-80;
  }else if(change>=60&&cid[7][1]>=60){
    hand.push(["TWENTY", 60]);
    change=change-60;
  }else if(change>=40&&cid[7][1]>=40){
    hand.push(["TWENTY", 40]);
    change=change-40;
  }else if(change>=20&&cid[7][1]>=20){
    hand.push(["TWENTY", 20]);
    change=change-20;
  }
  if(change>=10&&cid[6][1]>=10){
    hand.push(["TEN", 10]);
    change=change-10;  
  }
  if(change>=5&&cid[5][1]>=5){
    hand.push(["FIVE", 5]);
    change=change-5;  
  }
for(var i=0; i<4; i++){
  if(change>=1&&cid[4][1]>=1){
    hand.push(["ONE", 1]);
    change=change-1; 
    cid[4][1]=cid[4][1]-1; 
  }
}
  if(change>=0.75&&cid[3][1]>=0.75){
    hand.push(["QUARTER", 0.75]);
    change=change-0.75;
  }else if(change>=0.50&&cid[3][1]>=0.50){
    hand.push(["QUARTER", 0.50]);
    change=change-0.50;
  }else if(change>=0.25&&cid[3][1]>=0.25){
    hand.push(["QUARTER", 0.25]);
    change=change-0.25;
  }
for(var i=0; i<4; i++){
  if(change>=0.1&&cid[2][1]>=0.1){
    hand.push(["DIME", 0.1]);
    change=change-0.1;
    cid[2][1]=cid[2][1]-0.1;
  }
}
  if(change>=0.05&&cid[1][1]>=0.05){
    hand.push(["NICKEL", 0.05]);
    change=change-0.05;  
  }
for(var i=0; i<4; i++){
    if(change>=0.01&&cid[0][1]>=0.01){
    hand.push(["PENNY", 0.01]);
    change=change-0.01;
    cid[0][1]=cid[0][1]-0.01;
  }  
}

if(change>0) return {status: "INSUFFICIENT_FUNDS", change: []};

  console.log("change "+change+" "+hand);

  return {status: dstatus, change: [...hand]};
}

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36.

Challenge: Cash Register

Link to the challenge:

try adding console.log(tcid)

you are always assigning a negative value to change, in all the lines about it

You can use this tool for seeing what’s going on in your code, if you have not yet learned to use console.log in a way that works for debugging: http://pythontutor.com/javascript.html

1 Like