Cash Register test the console log looks correct but fails 3rd test

Hi all,

I know the code below is probably really bad but I am hoping you can explain why Iam failing the 3rd test down, all the others are passing, and as far as I can I see with console.log() the output is what the test wants.

function checkCashRegister(price, cash, cid) {
  let change = {status: "", change: []}
 let sum = cash-price
  const value = [0.01,0.05,0.1,0.25,1,5,10,20,100]
  let cidTotalValue = cid.reduce((total,cidValue)=> total + cidValue[1] ,0)
  let cid2 = cid.map(x=> x[1])
  let total = [0,0,0,0,0,0,0,0,0]
  let check = Math.round(sum * 100)/100
let i = 8
  num(sum,cid2,value)
function num(sum,cid2,value){


if(i<0){

}
    if(cid2[i]>0 && check-value[i]>=0){
     total[i] +=value[i]
     check-=value[i]
     cid2[i]-=value[i]
         check = (Math.round(check * 100)/100)
         total[i] = (Math.round(total[i] * 100)/100)
         num(sum,cid2,value)
    }
    else if(i>0){
          i--
     num(sum,cid2,value)
     } 
}

   let totalValue = total.reduce((total,value)=>  total+value )



console.log(totalValue,total,sum)



inject(sum)
function inject(sum){

if(cidTotalValue==sum){
change.status = "CLOSED"
change.change = cid

} else if(totalValue<sum){
  change.status = "INSUFFICIENT_FUNDS"
  change.change = []
} else if(totalValue==sum){
  change.status = "OPEN"
}

for(let i in total){
  if (total[i] >0 && totalValue==sum && change.status != "CLOSED" ) {
   
change.change.push([cid[i][0], total[i]])}
}

}
    console.log(change)
}

checkCashRegister(3.26, 100, [["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.90 Safari/537.36.

Challenge: Cash Register

Link to the challenge:

The console log:
totalValue, total, sum and change


96.74 [ 0.04, 0, 0.2, 0.5, 1, 15, 20, 60, 0 ] 96.74
{ status: 'OPEN',
  change: 
   [ [ 'PENNY', 0.04 ],
     [ 'DIME', 0.2 ],
     [ 'QUARTER', 0.5 ],
     [ 'ONE', 1 ],
     [ 'FIVE', 15 ],
     [ 'TEN', 20 ],
     [ 'TWENTY', 60 ] ] }

The array in the result object is backwards. The list should be:

[["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]

Largest denomination first.

Changing this line will fix:

change.change.push([cid[i][0], total[i]])}
1 Like

Thank you so much! I looked at this for a good hour. As the other results were printed backwards as I assumed that it was the correct way. Looking at the test criteria now I see it wanted the results two different ways . Well if it was a reading test i would have failed :smiley: Thanks again. :slight_smile:

Np. Glad I could help!

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