Cash Register - condition problem

Cash Register - condition problem
0

#1

Tell us what’s happening:
I’m working on Cash Register project and managed to write a complete code.
It works as expected except one condition which is:

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]]) should return {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]} .

I tried to console.log the problem and noticed that for some reason
if(cashInDrawerValue > baseChange)
is validated as false in this case and I have no idea why.

Any help will be appreciated. Thanks in advance.

Your code so far


function checkCashRegister(price, cash, cid) {
  // Variables
  var change = cash - price
  var baseChange = (cash - price).toFixed(2);
  var cashInDrawer = cid.reverse();
  var cashInDrawerValue = 0;
  var changeRegister = [];
  var changeRegisterValue = 0;
  // Functions
  var sumCashRegisterValue = function(arr) {
    var value = 0;
    for(var i = 0; i < arr.length; i++) {
      if(arr[i][0] !== undefined) {
        value += arr[i][1];
      }
    }
    return value.toFixed(2);
  }
  cashInDrawerValue = sumCashRegisterValue(cashInDrawer);
  
  // Find change
  if(cashInDrawerValue > baseChange) {
    for(var i = 0; i < cashInDrawer.length; i++) {
      if(cashInDrawer[i][0] === "ONE HUNDRED" && change >= 100 && change <= cashInDrawer[i][1]) {
        changeRegister.push(["ONE HUNDRED", change - change % 100]);
        change = change % 100;
      }
      if(cashInDrawer[i][0] === "TWENTY" && change >= 20 && change <= cashInDrawer[i][1]) {
        changeRegister.push(["TWENTY", change - change % 20]);
        change = change % 20;
      }
      if(cashInDrawer[i][0] === "TEN" && change >= 10 && change <= cashInDrawer[i][1]) {
        changeRegister.push(["TEN", change - change % 10]);
        change = change % 10;
      }
      if(cashInDrawer[i][0] === "ONE" && change >= 1 && change <= cashInDrawer[i][1]) {
        changeRegister.push(["ONE", change - change % 1]);
        change = change % 1;
      }
      if(cashInDrawer[i][0] === "QUARTER" && change >= 0.25 && change <= cashInDrawer[i][1]) {
        changeRegister.push(["QUARTER", change - change % 0.25]);
        change = (change % 0.25).toFixed(2);
      }
      if(cashInDrawer[i][0] === "DIME" && change >= 0.1 && change <= cashInDrawer[i][1]) {
        changeRegister.push(["DIME", change - change % 0.1]);
        change = (change % 0.1).toFixed(2);
      }
      if(cashInDrawer[i][0] === "NICKEL" && change >= 0.05 && change <= cashInDrawer[i][1]) {
        changeRegister.push(["NICKEL", change - change % 0.05]);
        change = (change % 0.05).toFixed(2);
      }
      if(cashInDrawer[i][0] === "PENNY" && change >= 0.01 && change <= cashInDrawer[i][1]) {
        changeRegister.push(["PENNY", change - change % 0.01]);
        change = (change % 0.01).toFixed(2);
      }
    }
  }

  changeRegisterValue = sumCashRegisterValue(changeRegister);

  if(baseChange == cashInDrawerValue) {
    return {status: "CLOSED", change: cashInDrawer.reverse()};
  }
  if(baseChange == changeRegisterValue) {
    return {status: "OPEN", change: changeRegister};
  }
  return {status: "INSUFFICIENT_FUNDS", change: []};
}

//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/69.0.3497.100 Safari/537.36.

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


#2

Hint: .toFixed() returns a string

Good luck!


#3

Thanks for pointing the right direction! I was so convinced that a Number method will return a number that I didn’t even check the type of returned data. My if loops need some more work too but I managed to finish the whole algorithm :slight_smile: Cheers!