Cash register puzzle 😵

So i have been working lately with JS, and i am a complete beginner, and a very sloppy code writer, and recently the last challenge of JS. and this has got me puzzled so bad, i just cant do any thing about it. So please help me out here. this is my last problem in JS.

Well, what have you tried so far?

ok, this might sound weird but i have tried if else to cut down the remaining amount i. e, the amount left to payback , into various array. which worked fine on its own, but for second part of the problem which ask to check cash in register and then make the left amount from it has me puzzled. To note, i used simpler JS methods like loops, if n else, nothing too mainstream, cause i didn’t get that stuff as clearly as i did for the earlier parts.

This will be far easier if you show the code you’ve written.

how to show you , i mean via codepen, or is there some link in the editor here on website??

function checkCashRegister(price, cash, cid) {
  let a = cash - price
  a = 96.74
  var change;
  let v = []

for(let i = 0;i<10;i++){
  
  if(20<a){



    let u = []
    u.push("TWENTY")
    let b = parseInt(a/20)
    let r = b*20
    u.push(r)
    a -= r
    v.push(u)
    
   
        if(a==0){break}




  }else if(10<=a){
    let b = parseInt(a/10)
    let u = []
    u.push("TEN")
    let r = b*10
    u.push(r)
    v.push(u)
    a = a-b*10
    if(a == 0){
      break
    }

   }else if(5<=a){
    let b = parseInt(a/5)
    let u = []
    u.push("FIVE")
    let r = b*5
 
    u.push(r)
    v.push(u)
    a = a-b*5
        if(a == 0){
      break
    }

   }else if(1<=a){
    let b = parseInt(a/1)
    let u = []
    u.push("ONE")
    let r = b*1
    u.push(r)
    v.push(u)
    a = a-b*1
        if(a == 0){
      break
    }


  }else if(0.25<=a){
    let b = parseInt(a/0.25)
    let u = []
    u.push("QUARTER")
    let r = b*0.25
    u.push(r)
    v.push(u)
    a = a-b*0.25
        if(a == 0){
      break
    }



  }else if(0.1<=a){
    let b = parseInt(a/0.1)
    let u = []
    u.push("DIME")
    let r = b*0.01
    u.push(r)
    v.push(u)
    a = a-b*0.1
        if(a == 0){
      break
    }



  }else if(0.05<= a){
   let b = parseInt(a/0.05)
   let u = []
   u.push("NICKEL")
    let r = b*0.05
    u.push(r)
   v.push(u)
   a = a-b*0.05
       if(a == 0){
      break
    }


  }else if(0.01<= a){
   let b = parseInt(a/0.01)
   let u = []
   u.push("PENNY")
   let r = b*0.01
  u.push(r)
   v.push(u)
   a = a - b*0.01
       if(a === 0){break}

  }
}

  return v;
}

console.log(checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 10], ["TWENTY", 60], ["ONE HUNDRED", 100]]))

it produces these results,

[ [ 'TWENTY', 80 ],
  [ 'TEN', 10 ],
  [ 'FIVE', 5 ],
  [ 'ONE', 1 ],
  [ 'QUARTER', 0.5 ],
  [ 'DIME', 0.02 ],
  [ 'PENNY', 0.03 ] ]

tho i am also unsure as to why it is breaking pennies as 3 not 4, like the given number should ask,

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