Algorithms and Data Structures Projects: Cash Register unable to verify the differences

Tell us what’s happening:

I can’t figure out how the (change > totalCash) is getting true while change is 96.74 and total cash is 335.41

Your code so far


function checkCashRegister(price, cash, cid) {
const fundStatus = {open:"OPEN",closed:"CLOSED",insufficient:"INSUFFICIENT_FUNDS"}

const CurrencyUnit = {
  "PENNY": 0.01,
  "NICKEL":0.05,
  "DIME":0.1,
  "QUARTER":0.25,
  "DOLLAR":1,
  "FIVE":5,
  "TEN":10,
  "TWENTY":20,
  "HUNDRER":100
}

var change = (cash-price).toFixed(2);
var totalCash = totalCashInDrawer(cid);
var changeObj = {status:"",change:[]}
console.log(change+":"+totalCash)

if(change > totalCash){
  changeObj.status = fundStatus.insufficient;
  changeObj.change = [];
}

if(change == totalCash){
  changeObj.status = fundStatus.closed;
  changeObj.change = cid;
}
/The below code may require some more changes/
if(change < totalCash){
  
  let total = [];
  for(let j=cid.length-1;j>=0;j--){
    let count =1;
    while(change >= CurrencyUnit[cid[j][0]] && change <= cid[j][1]){
      change -= cid[j][1];
      count++;
      console.log(count)
      total.push([cid[j][0],count*CurrencyUnit[cid[j][0]]]);
    }
    
  }
  if(total != ""){
    changeObj.status = fundStatus.open;
  }else{
    changeObj.status = fundStatus.insufficient;
  }
 
 changeObj.change = total; 
}
return changeObj;

}

function totalCashInDrawer(cashInDrawer){
var total = 0;
for(let i=0;i<cashInDrawer.length;i++){
  total+=cashInDrawer[i][1];
}

return total.toFixed(2);
}

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/87.0.4280.88 Safari/537.36 Edg/87.0.664.66.

Challenge: Cash Register

Link to the challenge:

Because you’re comparing strings and strings are compared alphabetically.

1 Like

Thank you so much for looking into it.