Cash register , loopoing issue

not accepting loop condition:
Hi there . I’m a bit puzzled, why loop seem to omit equality in comparison.

function checkCashRegister(price, cash, cid) {
let change = (cash-price)*100; console.log(change);
let vals = [1,5,10,25,100,500,1000,2000,10000]; console.log(vals);
let register=cid.map(a=> Math.round(a[1]*100)); console.log(register);
let cashTotal= register.reduce((a,b)=> a+b); console.log(cashTotal);

let outcome =[ ];

  let value =0;
  if(change > cashTotal){
    outcome.status = 'INSUFFICIENT_FUNDS'; 
    outcome.change = [];
    return outcome;
   } else if( change === cashTotal) {
    outcome.status ='CLOSED';
    outcome.change = cid;
    return outcome;
  } else {
    /*not looping properly
    ( 'change' does not accept 'OR EQUAL >= vals[a]' condition in loop, just skips to lower decrement omitting equality???) */
    
  for (let a = vals.length -1; a>=0; a--) {
   while( cid[a][1] > 0 && change >= vals[a]) {
     console.log(change)

    change -= vals[a]; 
    console.log([change]);

    cid[a][1] -= vals[a]; 
    value += vals[a]; console.log(value);
  } 
  if(value) {
    outcome.status ='OPEN';
    outcome.change=[cid[a][0], value];
  }
  }
}
return outcome;
}

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]]));

function checkCashRegister(price, cash, cid) {
let change = (cash-price)*100; console.log(change);
let vals = [1,5,10,25,100,500,1000,2000,10000]; console.log(vals);
let register=cid.map(a=> Math.round(a[1]*100)); console.log(register);
let cashTotal= register.reduce((a,b)=> a+b); console.log(cashTotal);

let outcome =[];

  let value =0;
  if(change > cashTotal){
    outcome.status = 'INSUFFICIENT_FUNDS'; 
    outcome.change = [];
    return outcome;
   } else if( change === cashTotal) {
    outcome.status ='CLOSED';
    outcome.change = cid;
    return outcome;
  } else {
    /*not looping properly
    ( 'change' does not accept 'OR EQUAL >= vals[a]' condition in loop, just skips to lower decrement omitting equality???) */
    
  for (let a = vals.length -1; a>=0; a--) {
   while( cid[a][1] > 0 && change >= vals[a]) {
     console.log(change)

    change -= vals[a]; 
    console.log([change]);

    cid[a][1] -= vals[a]; 
    value += vals[a]; console.log(value);
  } 
  if(value) {
    outcome.status ='OPEN';
    outcome.change=[cid[a][0], value];
  }
  }
}
return outcome;
}

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]]));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36.

Challenge: Cash Register

Link to the challenge:

Hi @sylvek7 ,

From what I can see in the above 2 lines, I think vals[a] should be brought back to the original format as below:
cid[a][1] -= vals[a]/100;
value += vals[a]/100;

thanks
from what i can see it is accepting values but during looping it willingone condition that change is equal = to vals[a], instead just proceeds to next iteration; decrementing to to lower value in vals[a]

for what I see, you are using
cid[a][1] in dollars, but change and vals[a] are in pennies

so you have that before the line cid[a][1] -= vals[a]
you have
'cid[a][1]': 4.25, 'vals[a]': 25
and after
'cid[a][1]': -20.75

so you maybe should alway use pennies instead of dollars

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.