Cash Register / problems with my code help!

Hi everyone! Please help me with my cash register. This is my code:

var denom = [
  { name: "ONE HUNDRED", value: 100.0 },
  { name: "TWENTY", value: 20.0 },
  { name: "TEN", value: 10.0 },
  { name: "FIVE", value: 5.0 },
  { name: "ONE", value: 1.0 },
  { name: "QUARTER", value: 0.25 },
  { name: "DIME", value: 0.1 },   
  { name: "NICKEL", value: 0.05 },
  { name: "PENNY", value: 0.01 }
];
function checkCashRegister(price, cash, cid) {
  let change = cash - price;
  let result = {
    status: "",
    change: []
  }
  let sumCid = 0;
  for(let element of cid) {
  sumCid += element[1];
  }
  if(sumCid < change) {
    result.status = "INSUFFICIENT_FUNDS";
    return result;
  }
  if(sumCid === change) {
    result.status = "CLOSED"
    result.change = cid;
    return result;
  }
  let newResult = [];
  cid = cid.reverse();
  let val = 0;
  for(let i = 0; i < cid.length; i++) {
   while(cid[i][1] > 0 && change >= denom[i].value) {
  
   change -= denom[i].value
   change = change.toFixed(2);
   cid[i][1] -= denom[i].value
   val += denom[i].value;
   }
   if(val > 0) {
   newResult.push([cid[i][0], val])
   }
  }
return newResult;
}
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]]));

What’s wrong with my newResult array? Why it is like that: [ [ 'QUARTER', 0.5 ], [ 'DIME', 0.5 ], [ 'NICKEL', 0.5 ], [ 'PENNY', 0.5 ] ] ? I need only [["QUARTER", 0.5]]. Is it wrong something in this part of my code:

let newResult = [];

 cid = cid.reverse();

 let val = 0;

 for(let i = 0; i < cid.length; i++) {

  while(cid[i][1] > 0 && change >= denom[i].value) {

 

  change -= denom[i].value

  change = change.toFixed(2);

  cid[i][1] -= denom[i].value

  val += denom[i].value;

  }

  if(val > 0) {

  newResult.push([cid[i][0], val])

  }

 }

return newResult;

?

where you check if val>0 is in the wrong place…
that is why your getting that answer. if you put it in the right place you will get the right amount of quarters. though… if you want to combine them like you show you will also have to fix something else…

copy paste this code is the same as yours just with a few added logs and ive moved that thing you asked about above to a better place. maybe this will help you debug:

var denom = [
  { name: "ONE HUNDRED", value: 100.0 },
  { name: "TWENTY", value: 20.0 },
  { name: "TEN", value: 10.0 },
  { name: "FIVE", value: 5.0 },
  { name: "ONE", value: 1.0 },
  { name: "QUARTER", value: 0.25 },
  { name: "DIME", value: 0.1 },   
  { name: "NICKEL", value: 0.05 },
  { name: "PENNY", value: 0.01 }
];
function checkCashRegister(price, cash, cid) {
  let change = cash - price;
  let result = {
    status: "",
    change: []
  }
  let sumCid = 0;
  for(let element of cid) {
  sumCid += element[1];
  }
  if(sumCid < change) {
    console.log('flag1')
    result.status = "INSUFFICIENT_FUNDS";
    return result;
  }
  if(sumCid === change) {
    console.log('flag2')
    result.status = "CLOSED"
    result.change = cid;
    return result;
  }
  // console.log('flag3')
  let newResult = [];
  cid = cid.reverse();
  let val = 0;

  console.log(denom, "=denom")
  console.log(cid.length,"= cid length")
  for(let i = 0; i < cid.length; i++) {
  console.log('flag4')
     console.log(denom[i].value, '=denomi val')
     console.log(cid[i][1], '=cid 1 val')

   while(cid[i][1] > 0 && change >= denom[i].value) {
   console.log('flag5')
   change -= denom[i].value
   change = change.toFixed(2);
   cid[i][1] -= denom[i].value
   val += denom[i].value;
   console.log(val, '=val in the cid loop')
      if(val > 0) {
  console.log('flag6')
   newResult.push([cid[i][0], val])
   }
   }

  }
 console.log(newResult[0],newResult[1],newResult[2],newResult[3], "newResu");
 console.log(newResult, "newResult")
}
console.clear()
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]]);

also there could be some other issues with your code but… i think the above is a good start.

Edit:
afterwards I noticed the advice above i gave is not so good. I did not notice you still have [["QUARTER", 0.25], ["QUARTER", 0.5]] and you only wanted [[“QUARTER”, 0.5]]

So, what should i do? (you told your advice is not so good). Please, can you give me another advice?

yes, I did notice when I tested your code it did not return it in the format that it is expecting an object with a status key and a change key. I will try to give better help tomorrow to help debug if youve not already solved or if someone else did not answer with better advice. i apologize for the confusion, i thought I knew this challenge better than I did, I must review it as well. humbled.

Ok i will fix the bad advice I gave before… check this out:
your variable you can not put it where it currently is… and
by that I mean the place where you are initializing it to zero is incorrect. fix that and it will work as you expect… and as I mentioned above you will still have to fix the form in the way its returned as the test is expecting an object with a status key and a change key.

importantly for now reconsider where you are putting this line:

 let val = 0;

you get that output because of where you initialize val variable. in your logic it is greater than zero for 4 out of the 9 iterations.

what that means is that you dont start accruing value until you get to the quarters, and that is good, because youve said this:

while(cid[i][1] > 0 && change >= denom[i].value) etc....

as long as you have some of that particular bill/coin/currency value i.e. its not zero, and the change that you owe is greater than or equals the coin value… like you cant pay the customer back with a bill or coin that is greater than what you owe… so that is correct…

however whats not good is you dont stop there and you continue to push the next denomination in your iteration while pushing the same value…because after all “I variable” will still continue to advance…

I hope that all makes sense, and now its clear. if you fix where you put val variable it s should fix all these problems

1 Like

Thanks you very much!

I understood correctly: my for loop runs at different denominations because they correspond to the condition val> 0. If I declare a variable inside the for loop, will the loop finish executing when I give change?

If I declare a variable inside the for loop

you should initialize val variable inside the for loop.

will the loop finish executing when I give change?

the loop finishes executing after it count down all the subarrays in your cid array there are 9 of them for what you passed. you can console.log cid.length . afterwar it finishes youve made newResult available in an outer scope and youve returned it. There is nothing inside your for loop that would cause it to return early. You must format what you are returning to look like what the challenge is expecting. You are using greedy method

Look, it is working now, thanks you!

var denom = [
  { name: "ONE HUNDRED", value: 100.0 },
  { name: "TWENTY", value: 20.0 },
  { name: "TEN", value: 10.0 },
  { name: "FIVE", value: 5.0 },
  { name: "ONE", value: 1.0 },
  { name: "QUARTER", value: 0.25 },
  { name: "DIME", value: 0.10 },   
  { name: "NICKEL", value: 0.05 },
  { name: "PENNY", value: 0.01 }
];
function checkCashRegister(price, cash, cid) {
  let change = cash - price;
  let result = {
    status: "",
    change: []
  }
  let sumCid = 0;
  for(let element of cid) {
  sumCid += element[1];
  }
  if(sumCid < change) {
    result.status = "INSUFFICIENT_FUNDS";
    return result;
  }
  if(sumCid === change) {
    result.status = "CLOSED"
    result.change = cid;
    return result;
  }
  let newResult = [];
  cid = cid.reverse();
  for(let i = 0; i < cid.length; i++) {
  let val = 0;
  while(change >= denom[i].value && cid[i][1] > 0) {
  cid[i][1] -= denom[i].value;
  change -= denom[i].value
  change = change.toFixed(2);
  val += denom[i].value
  }
  if(val > 0) {
      newResult.push([cid[i][0], val])
    }
}
if(change > 0) {
  result.status = "INSUFFICIENT_FUNDS"
  return result;
}
result.status = "OPEN"
result.change = newResult
return result;
}
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]]));
1 Like

excellent. and now its clear why it was returning that answer before yes? if you have any other questions about it please ask.