Cash Register project console error

Tell us what’s happening:
Hi I cant pass the Cash Register project even if the results are correct
// running tests

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

should return

{status: "OPEN", change: [["QUARTER", 0.5]]}

code result : { status: ‘OPEN’, change: [ [ ‘QUARTER’, 0.5 ] ] }

Your code so far

let a = [ 100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01 ];
let t = [ 'PENNY','NICKEL','DIME','QUARTER','ONE','FIVE','TEN','TWENTY','ONE HUNDRED' ];
function checkCashRegister(price, cash, cid) {
let res = {status : "", change: {}};
for(let i in t.reverse()){
  res.change[t[i]] = 0;
}
let c = cid.reverse().reduce(function(x,y,i){x.push(Math.round(y[1]/a[i])); return x;},[]);
let chng = parseInt((cash-price)*100);
  for(let i in a){
    a[i] = a[i]*100;
    if(chng % a[i] == 0 && chng > 0 && c[i] > 0){
      let y = 0;
      while(chng % a[i] == 0 && chng > 0 && c[i] > 0){
      chng = chng - a[i];
      c[i] = c[i] - 1;
      res.change[t[i]] += (a[i])/100;
      }
    }
    else if(chng % a[i] < chng && chng % a[i] > 0 && c[i] > 0){
    let p = parseInt(chng / a[i]);
    if(p > c[i]){ p--};
      while(chng % a[i] < chng && chng > 0 && c[i] > 0){
        chng = chng - a[i] * p;
        c[i] = c[i] - p;
        res.change[t[i]] += (a[i] * p) / 100;
      }
    }
}
for(let i in t.reverse()){
  res.change[t[i]] = Number(res.change[t[i]].toExponential(3));
}
let sum = 0;
for(let i in res.change){
  sum += res.change[i];
}
if(sum.toPrecision(4) == cash - price && c.reduce((x,y) => x + y ) > 0){
  res.status = "OPEN";
  for(let i in res.change){
    if(res.change[i] == 0){
      delete res.change[i];
    }
  }
}
if(sum.toPrecision(4) == cash - price && c.reduce((x,y) => x + y ) == 0){
  res.status = "CLOSED";
}
if(sum.toPrecision(4) < cash - price){
  res.status = "INSUFFICIENT_FUNDS";
  for(let i in res.change){
    delete res.change[i];
  }
}
let tempchng =[];
for(let i in res.change){
  tempchng.push([i,res.change[i]])
}
if(res.status == "CLOSED"){res.change = tempchng.reverse();}
else{res.change = tempchng;}
return res;
}
console.log({status: "OPEN", change: [["QUARTER", 0.5]]})
let w = 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]]);
console.log(w);
  **Your browser information:**

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

Challenge: Cash Register

Link to the challenge:

Notice two things - a and t are defined outside of function, at the same time a is modified later in the function. This one is interesting, as you can see here with your own eyes, how unexpected side-effects may occur, when using globally defined variables.

1 Like

Got it, thanks bro! appreciate your time!

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