Basic logic not working?

I’m working on the ‘Cash Register’ algorithm project, and I have all my tests passing except this one:

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]]}

When I console log the amounts through the code for these parameters, I see that the changeRequired (cash given - price) = 96.74, and the drawerAmountTotal (amount in the drawer) = 335.41.

However, when I run this ‘if’ statement:
'if (changeRequired > drawerAmountTotal){

}

it returns ‘true’ and runs the statement. How is this possible? In what universe is 96 larger than 335?

Here’s my code for the problem:

// Create an object that represents each money name

// and its corresponding number value:

const moneyValues = {

  "PENNY": .01,

  "NICKEL": .05,

  "DIME": .1,

  "QUARTER": .25,

  "ONE": 1.00,

  "FIVE": 5.00,

  "TEN": 10.00,

  "TWENTY": 20.00,

  "ONE HUNDRED": 100.00

}

 

const checkCashRegister = (price, cash, cid) => {

  // Initialize answer 'ans' variable as a basic object:

  let ans = {

    status: '',

    change: []

  };

 

  // Initialize variable for amount to be given to customer 

  // (rounded to 2 decimal places):

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

 

  // Add all amounts in 'cid' to see what the drawer's total is:

  let drawerAmountTotal = cid.reduce((accumulator, element) => {

    return accumulator + element[1];

  }, 0).toFixed(2);

 

  // Initialize an object that will represent each value given in 'cid':

  let cashInDrawer = {};

 

  // Add all values of parameter 'cid' to the cashInDrawer object:

  for (let i = 0; i < cid.length; i++){

    cashInDrawer[cid[i][0]] = cid[i][1];

  }

 

  // Initialize obj for cash to give:

  let cashToGive = {}

 

  // Fill that with values that === 0

  for (let i = 0; i < cid.length; i++){

    cashToGive[cid[i][0]] = 0;

  }

  // Make a big ol' if statement:

  // If statement for if there isn't enough money:

  console.log(changeRequired)

  console.log(drawerAmountTotal)

  if(changeRequired > drawerAmountTotal){

    ans.status= 'INSUFFICIENT_FUNDS';

    // If statement for if the amount in the cash drawer

    // is the exact amount the customer needs:

  }else if(changeRequired === drawerAmountTotal){

    ans.status = 'CLOSED';

    ans.change = [...cid];

    // if statement for if there is more than enough

    // money in the till for the change:

  }

  else {

    // For loop to go through each value (starting with 'ONE HUNDRED')

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

      // If changeRequired minus the current value is still >= 0...

      if(changeRequired - moneyValues[cid[i][0]] >= 0){

        

        // If there's enough cash in the drawer to withdraw that amount...

        if(cashInDrawer[cid[i][0]] - moneyValues[cid[i][0]] >= 0){

          // Decrease 'changeRequired' by given value:

          changeRequired -= moneyValues[cid[i][0]];

          // Decrease 'cashInDrawer' by given value:

          cashInDrawer[cid[i][0]] -= moneyValues[cid[i][0]];

          // Increase 'CashToGive' by given value:

          cashToGive[cid[i][0]] += moneyValues[cid[i][0]]

          // Revert 'i' by 1 so we'll test this value again

          // (In case we can take out multiple of the same money value)

          i++;

        }

      }

    }

    // Check if changeRequired got taken down to 0.

    // If not, set to "insufficient funds" ans:

    if (changeRequired > 0){

      ans.status='INSUFFICIENT_FUNDS'

    }else{

      // set 'ans' variable with 'open' status and new cashInDrawer

      // in the form of an array

      ans.status = 'OPEN';

      for (var val in cashToGive){

        if(cashToGive[val] > 0){

          ans.change.push([val, cashToGive[val]])

      }

    }

    }

    

  }

  // return 'ans' variable:

  console.log(ans)

  return ans;

}

I wonder if it is a goobery consequence of toFixed actually turning the number into a string.

Personally, I find it easier to use an integer number of cents to represent the quantities.


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Thank you, that worked! And thanks for fixing my post, I’ll make sure to format my code right in future posts :+1: :+1:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.