JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:
When I test it out it puts out nickels even though they want just the quarters. Please help me not make these errors.

Your code so far

function checkCashRegister(price, cash, cid) {
  let total =0.0;
  for(let i of cid){
    total+=i[1]
    total*=100
    total = Math.round(total)
    total/=100
  }
  let change=cash-price;
  if(total<change){
    return {status: "INSUFFICIENT_FUNDS", change: []}
  }else if (total==change){
    return {status: "CLOSED", change: cid}
  }else{
    let addList=[]
    let s100=0
    let s20=0
    let s10=0
    let s5=0
    let s1=0
    let c25=0.00
    let c10=0.0
    let c5=0.00
    let c1=0.00
    for(let a=100; a<=change; a+=100){
      s100+=100
    }
    for(let b=20; b<=change%100-s100; b+=20){
      s20+=20
    }
    for(let c=10; c<=change%20-s20; c+=10){
      s10+=10
    }
    for(let d=5; d<=change%10-s10; d+=5){
      s5+=5
    }
    for(let e=1; e<=change%5-s5; e+=1){
      s1+=1
    }
    for(let f=0.25; f<=change%1-s1; f+=0.25){
      c25+=0.25
    }
    for(let g=0.1; g<=change%0.25-c25; g+=0.1){
      c10+=0.1
    }
    for(let h=0.05; h<=change%0.1-c10; h+=0.05){
      c5+=0.05
    }
    for(let i=0.01; i<change%0.05-c5; i+=0.01){
      c1+=0.01
    }
    if(s100>0){
      addList.push(["ONE HUNDRED", Math.round(s100*100)/100])
    }
    if(s20>0){
      addList.push(["TWENTY", Math.round(s20*100)/100])
    }
    if(s10>0){
      addList.push(["TEN", Math.round(s10*100)/100])
    }
    if(s5>0){
      addList.push(["FIVE", Math.round(s5*100)/100])
    }
    if(s1>0){
      addList.push(["ONE", Math.round(s1*100)/100])
    }
    if(c25>0){
      addList.push(["QUATER", Math.round(c25*100)/100])
    }
    if(c10>0){
      addList.push(["DIME", Math.round(c10*100)/100])
    }
    if(c5>0){
      addList.push(["NICKEL", Math.round(c5*100)/100])
    }
    if(c1>0){
      addList.push(["PENNY", Math.round(c1*100)/100])
    }
    console.log({status: "OPEN", change: addList})
    return {status: "OPEN", change: addList}
  }
  //return total;
}

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/117.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

Take a look at the code (loop in this case) deciding if there’s need to add nickels. It seems the calculated value is not what it should be. Any ideas why that might happen?

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