Skip to content
Advertisement

Final value returned from a for loop is mysteriously multiplied by 2 – Javascript. How to fix this?

I am coding a cash register…here are the conditions (you can largely ignore these)

let dif100 = {
  rem : function(n){return n%10000},
  num: 10000,
  name : "ONE HUNDRED"} // 0
let dif20 = {
  rem : function(n){return n%2000},
  num: 2000,
  name : "TWENTY"}  // 1
let dif10 = {
  rem : function(n){return n%1000},
  num: 1000,
  name : "TEN"}   // 2
let dif5 = {
  rem : function(n){return n%500},
  num: 500,
  name: "FIVE"}     // 3
let dif1 = {
  rem : function(n){return n%100},
  num: 100,
  name: "ONE"}     // 4
let difqu = {
  rem : function(n){return n%25},
  num: 25,
  name: "QUARTER"} // 5
let difdime ={
  rem : function(n){return n%10},
  num: 10,
  name: "DIME"}//6
let difnic = {
  rem : function(n){return n%5},
  num: 5,
  name: "NICKEL"} //7
let difpen = {
  rem : function(n){return n%1},
  num: 1,
  name: "PENNY"} //8

let difArr = [dif100,dif20,dif10,dif5,dif1,difqu,difdime,difnic,difpen]

cid = [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1],
 ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20],
  ["TWENTY", 60], ["ONE HUNDRED", 100]]

let cashBox = cid.reverse()

for(let i = 0; i < cashBox.length; i++){
  cashBox[i][1] = Math.round(cashBox[i][1] *100)
}

everything is multiplied by 100 to avoid fractions

the value of dif is supposed to be the difference between cash and price * 100 .For now, I am manually assigning its value

When I run this function below with the value of dif assigned to 18000 ($180), it will check the cashbox to see if there’s any bills and return the available bill

dif = 18000
answerArr = []
  for(let i = 0; i < difArr.length; i++){
    if(dif < difArr[i].num){
      dif = dif
    } else if(difArr[i].rem(dif) == 0){ 
      if(cashBox[i][1] >= dif){
        answerArr.push([difArr[i].name, dif/100])
        dif = 0 
      } else if (cashBox[i][1] < dif){
        answerArr.push([difArr[i].name, cashBox[i][1]/100])
        dif = dif - cashBox[i][1]
      }
    }else {
      if(cashBox[i][1] >= dif - difArr[i].rem(dif)){
        answerArr.push([difArr[i].name, (dif - difArr[i].rem(dif))/100])
        dif = difArr[i].rem(dif)
      } else if (cashBox[i][1] < dif - difArr[i].rem(dif)){
        answerArr.push([difArr[i].name, cashBox[i][1]/100])
        dif = dif - cashBox[i][1] + difArr[i].rem(dif)
      }
    }
  }
answerArr //returns [
    [
        "ONE HUNDRED",
        100
    ],
    [
        "TWENTY",
        60
    ],
    [
        "TEN",
        20
    ]
]

However, when I put dif = 18100 ($181) The return is

[ [ “ONE HUNDRED”, 100 ], [ “TWENTY”, 60 ], [ “TEN”, 20 ], [ “ONE”, 2 ] ]

Basically the last dollar value is multiplied by 2. Why is this?

same if dif = 18050

[ [ “ONE HUNDRED”, 100 ], [ “TWENTY”, 60 ], [ “TEN”, 20 ], [ “ONE”, 1 ] ]

I understand it’s a long question. please don’t give any direct answer on how to better make a cash register(though advices and nudges toward a good direction would be greatly appreciated!!) . I am trying to do it on my own. Thank you very much! Please help!

Advertisement

Answer

The problem is in the last else if block. This is where the cash register has not enough for the current denomination, so the idea is to take all of them.

However, there is a mistake in the calculation of the money that remains to be covered:

    dif = dif - cashBox[i][1] + difArr[i].rem(dif)

The remainder should not be added separately, because that remainder is still in dif. So by adding it, dif will get that remainder twice instead of one.

The correction is just to subtract what is in the cash register for that denomination. There is no need to do anything with remainders:

    dif = dif - cashBox[i][1]
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement