JS Cash Register project help required

Hi,

I’m working on the Cash register project in the JS certification.

For some reason although the amount in ‘cash-in-drawer’ is greater than the ‘changeDue’, the IF statement (currencyAvailable < changeDue) is returning true (and returning the related output) even though respective values are 335.41 and 96.74 (checked immediately before conditional IF statement via console.log.

Obviously I must have an error somewhere in the code, but for the life of my I can think or see where.

Any help would be greatly appreciated. (I know I haven’t addressed the use case where change cant be made, planning to address that last).

here is my code:

function checkCashRegister(price, cash, cid) {

  const changeDue = (cash - price).toFixed(2);

  const currencyAvailable = getAvailableCurrency(cid);

  function getAvailableCurrency(changeInDrawer) {

   let total = 0;

   for (let change of changeInDrawer) {

     total += change[1];

   }

   return total.toFixed(2);

 }

 console.log("Currency Available: " + currencyAvailable);

 console.log("Change Due: " + changeDue);

 if (currencyAvailable < changeDue) {

   return {status: "INSUFFICIENT_FUNDS", change: []};

 }

 else if (currencyAvailable === changeDue) {

   return {status: "CLOSED", change: cid};

 }

 else {

   return {status: "OPEN", change: getChangeToReturn(changeDue, cid)};

 }

 function getChangeToReturn(changeDue, cid) {

   const change = [];

   const currencyNameValue = {

      PENNY: 0.01,

      NICKEL: 0.05,

      DIME: 0.10,

      QUARTER: 0.25,

      ONE: 1.00,

      FIVE: 5.00,

      TEN: 10.00,

      TWENTY: 20.00,

      "ONE HUNDRED": 100.00

   };

   for (let i = cid.length - 1; i >= 0; i--) {

     const currencyName = cid[i][0];

     const currencyTotal = cid[i][1];

     const currencyValue = currencyNameValue[currencyName];

     let currencyAmountToReturn = 0;

     let currencyAmount = (currencyTotal/currencyValue).toFixed(2);

     while (changeDue >= currencyValue && currencyAmount > 0) {

       changeDue -= currencyValue;

       currencyAmount--;

       currencyAmountToReturn++;

     }

     if (currencyAmountToReturn > 0) {

       change.push([currencyName, currencyAmountToReturn * currencyValue]);

     }

   }

   return change;

 }

}

/* let test1 = checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60],

["ONE HUNDRED", 100]]); */

let test2 = 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]]);

console.log(test2);

console log output:

Currency Available: 335.41
Change Due: 96.74
{ status: ‘INSUFFICIENT_FUNDS’, change: }

I don’t have time to run your code, but a quick glance…

You are comparing two strings. When JS compares strings, it checks the left most characters and keeps moving to the right until it finds a mismatch. So, when it sees:

('335.41' < '96.74')

JS says, “OK, the unicode character code for ‘3’ is less than that for ‘9’, so this is true.”

You’re converting to strings, presumably for the trailing zeros. You have a couple of options. One would be to wait until you display the numbers to convert to strings (so you would be comparing numbers). The other option would be to convert the strings to numbers for the comparison - the unary + operator is an easy way to do that.

1 Like

Ah thank you soo much! silly school boy error on my part, I’ve now changed the conditionals to be more like if(x - y < 0) {do this…}

thanks again & happy holidays!