Problem with javascript Cash register project

In the challenge of Cash Register Project, there’ s a condition that states like this:

When price is 19.5, the value in the #cash element is 20, cid is [[“PENNY”, 0.01], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 1], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]], and the #purchase-btn element is clicked, the value in the #change-due element should be Status: INSUFFICIENT_FUNDS.

And the condition for INSUFFICIENT_FUNDS looks like this

if (cashInDrawer < changeDue) {
changeDueDiv.textContent = ‘Status: INSUFFICIENT_FUNDS’;
}

With our case, I can see that “cashInDrawer” will be (1 + 0.01) = 1.01, and the changeDue will be (20 - 19.5) = 0.5;
With that, Why would it result in insufficient balance when the cashInDrawer is greater than the changeDue !!

I guess the Status would be OPEN instead; because according to the passing conditions, the code will never pass.

1.01 is of course higher than 0.5. However you cannot take bill that has denomination of 1 and torn it apart to get two 0.5 bills :slightly_smiling_face:. With money that is in the drawer it’s impossible to give out 0.5 change.

Wow! And now I am stuck with how to write that condition with my codes :smirk::smile:
I tried this but it didn’t pass.

if ((cashInDrawer < changeDue) || (changeDue > 0  &&  changeDue <= 1)) {
      changeDueDiv.textContent = 'Status: INSUFFICIENT_FUNDS';
}

Or may be I misunderstood

Now I have a conditions that looks like this

if ((cashInDrawer < changeDue) || (changeDue > 0 &&  changeDue <= 1)) {
      changeDueDiv.textContent = 'Status: INSUFFICIENT_FUNDS';
    } else if (cashInDrawer === changeDue) {
      let closedStatMessage = 'Status: CLOSED ';
      cid.forEach(([denomination, amount]) => {
        if (amount > 0) {
          closedStatMessage += denomination + ': $' + amount + ' ';
        }
      });
      changeDueDiv.textContent = closedStatMessage.trim();
    } else {
      changeDueDiv.textContent = 'Status: OPEN ' + change.map(el => el.join(': $')).join(' ');
    }
  }

I’ve edited your code 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 (').

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