Program duplicating last entry

Tell us what’s happening:
I’ve been working on this cash register project and I’m pretty close to finishing it, however for the second use case it test, it outputs 2 penny values (.03 and .01) rather than a single penny value of .04 . It seems to work fine with every other currency type and the simple problem with a change of .50 works fine, but I cant seem to get this specific use case to output .04 for the penny value. Any ideas on what could cause this? The rounding throughout the program is vital as without it the number will slowly deteriorate into .00999999999 for the decimal which breaks the whole program. Also cant round within the while loop where the value is calculated as this ends up rounding the decimal up to .75, causing it to output .75 in quarters.

Your code so far


function checkCashRegister(price, cash, cid) {
var change = cash - price;
var LowChange = function(){
  for (let i=0; i<changeValue.length;i++){
    if (changeValue[i] > change){
      topChangeIndex = i-1
      return topChangeIndex
    }
  }
}
var retObj = {status: "OPEN",change: []}
let totalChange = 0
let changeType = ["PENNY","NICKEL","DIME","QUARTER","ONE","FIVE","TEN","TWENTY","ONE HUNDRED"]
let changeValue = [.01,.05,.10,.25,1,5,10,20,100]
let topChangeIndex = 0
let counter = 0
//adds up all of the different cash types to give a total value of change
for (let i=0; i<cid.length;i++){
  totalChange += cid[i][1]
}
//rounds the final number to prevent random repeating numbers
totalChange = Math.round(100*totalChange)/100
// if the customer needs more change than there is, returns insufficient funds
if (totalChange < change){
  retObj.status = "INSUFFICIENT_FUNDS"
  retObj.change = []
  return retObj
}
else if (totalChange === change){
  retObj.status = "CLOSED"
  retObj.change = cid
  return retObj
}
else if (totalChange > change){
  while (change > 0){
    topChangeIndex = LowChange()
    counter = 0
    // rounds up periodically to prevent .01 from becoming .0099999999
    change = Math.ceil(100*change)/100
    while (changeValue[topChangeIndex] <= change){
      change -= changeValue[topChangeIndex]
      counter++
      cid[topChangeIndex][1] -= changeValue[topChangeIndex]
      if (cid[topChangeIndex][1] == 0){
        retObj.change.push([changeType[topChangeIndex],counter*changeValue[topChangeIndex]])
        topChangeIndex--
        counter = 0
      }
      else if (change < changeValue[topChangeIndex]){
          retObj.change.push([changeType[topChangeIndex],counter*changeValue[topChangeIndex]])
        }
      }
    }
  }
  return retObj
}


console.log(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/83.0.4103.61 Safari/537.36.

Challenge: Cash Register

Link to the challenge:

Hi Zie62,

I ran into a similar issue and received this helpful tip:

1 Like

Ahhhh, very helpful. I finished it with a kind of cheesy case-specific solution but if I am writing a similar code in the future that NEEDS to be flexible, this will save my life!