Cash Register crises

Tell us what’s happening:
my if statements are clashing, when there is cash in the regCash.total yet that cash is less then the change my code can’t return object with "obj.status = "“INSUFFICIENT_FUNDS” & obj.change = [empty ], also the, it won’t return object with "obj.status = “CLOSED” & obj.change = cid, when there is some cash in my regCash but less then the change.

any tips on how I can fix this ?
please point out where my code maybe doing funny stuff that I obviously cannot see.

Your code so far


function checkCashRegister(price, cash, cid) {

let change = cash - price;
  change = change.toFixed(2)

// const changeClone = change

let regCash = cid.reduce((acc,val) => {
          acc.total += val[1]
          acc[val[0]] = val[1]
          
          return acc
          },{total:0,})

  regCash.total = regCash.total.toFixed(2)

  
const obj = {status:"", change:[]}

 


 let calculate =checkCashRegister.currencyUnit.reduce((acc,unit)=> {
              
              let amtOwed = 0
              let returnArr = []
              while(regCash[unit[0]] > 0 && change >= unit[1]){
                
               change -= unit[1]
               regCash[unit[0]] -= unit[1]
               amtOwed += unit[1]
              
              change = Math.round(change*100)/100
              }
              
              if(amtOwed> 0 ){
             
              returnArr.push(unit[0],amtOwed)
              
               acc.push(returnArr) 
              
              }
              
             return acc
     },[])
     
 let totalChange = calculate.flat().filter(val => Number(val)).reduce((a,b) => a+b).toFixed(2)   
 
 if(regCash.total > change){
        obj.status = "OPEN"
        obj.change = calculate
    }

if(regCash.total < change){
        obj.status = "INSUFFICIENT_FUNDS"
    }

if(regCash.total === change){
        obj.status = "CLOSED"
        obj.change = [...cid]
    } 
  


return obj
 

    
         
}
// Currency Units:
checkCashRegister.currencyUnit =[["PENNY", 0.01],
["NICKEL", 0.05],
["DIME", 0.1],
["QUARTER", 0.25],
["ONE", 1],
["FIVE", 5],
["TEN", 10],
["TWENTY", 20],
["ONE HUNDRED", 100]].reverse()

console.log(checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register

So first thing I might suggest: the toFixed() is a great thing, but it changes the value from a number to a string. In each place where you use that to round your value, remember to wrap it with a Number(...) to convert it back to an actual number – otherwise you’re comparing the string value in change to the string value in regCash.total. The results will never be quite what you expect.

There are a few other logic points to consider:

  • When you have calculated the change due and the cash-on-hand (your change and regCash.total, respectively), you can check for insufficient funds or exact amount due cases, before you even get into calculating anything else. Remove those options early, leaving yourself exactly TWO cases to handle.

  • What are the two cases left to handle? :wink:

Also, oddly, I was testing this on repl.it, and it threw me an error for Array.prototype.flat() – be aware that the use of flat() may not be universal. Largely, I think, it’s because repl.it uses some sort of parser (or ‘linter’) to check code before running it, and that parser doesn’t recognize newer functionality. I got it to work with a workaround, then was able to do debugging.

So to recap: 1. be careful of toFixed(...), it changes your variable from a number to a string. and 2. remove logical branches as early as you can, leaving you fewer to deal with later. :wink: