-3

I cannot get the if else statement to work, the if statement and the else statement works,
I test the if else statement with an input of 115, it doesnt work,
but if i put 135 the if statement works as it should.
I need help to fix the if else statement please!!

var start = button.addEventListener("click", function() {
  var lbs = input.value - 45 /*weight without the bar*/
  var add = 0

  var plates45 = 0
  var plates35 = 0

  while (lbs != add) {
    if ((add += 90) <= lbs) {
      plates45 += 2
      add += 90
      forty.innerHTML = "45lbs: " + plates45
    } else if ((add += 70) <= lbs) {
      plates35 += 2
      add += 70
      thirty.innerHTML = "35lbs: " + plates35
    } else {
      add = lbs /*to prevent infinte loop*/
    }
  }
})
<h1 id="header">Plate Helper</h1>

<div id="container1">
  <p id="weight">Weight</p>
  <input type="text" id="input">
</div>
<div id="blue">
  <button id="button">Calculate</button>
</div>

<div id="container2">
  <center>
    <p id="result">Plates Needed</p>
    <div id="forty">
      <p id="forty_plate">45lbs: </p>
    </div>
    <div id="thirty">
      <p id="thirty_plate">35lbs: </p>
    </div>
    <div id="twenty">
      <p id="twenty_plate">25lbs: </p>
    </div>
    <div id="ten">
      <p id="ten_plate">10lbs: </p>
    </div>
    <div id="five">
      <p id="five_plate">5lbs: </p>
    </div>
    <div id="two">
      <p id="two_plate">2.5lbs: </p>
    </div>
  </center>

I refresh the page before each test so i dont think the first if statement affects the condition for the if else statement

Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
  • 1
    it's painful to read without the HTML part, you have to add it in your question https://stackoverflow.com/help/how-to-ask – Mister Jojo Aug 07 '22 at 02:08
  • The `+=` in the `if` statements will increase the value of the `add` variable. So, within the first execution of the loop `add` will change from `0` (at the start) to `90` (at the `if` clause), and then to `180` (within the `if` when executing `add += 90`), and then to `250` (at the `else if` clause). To get the `else if` statements to execute, please try with `input.value` as a high number say 1045 (or at least above 296). And, kindly do include the HTML as noted by @MisterJojo above. – jsN00b Aug 07 '22 at 02:11
  • i added the HTML – Yagami_Light Aug 07 '22 at 02:21

1 Answers1

0

The reason it doesn't work is the += operator in the if statements. The first one if ((add += 90) <= lbs){ first adds 90 to add, then checks if add is less than lbs. Then when it arrives at if ((add += 70) <= lbs){, add is already 90, so then it adds 70 (=160) and checks if it's less than lbs. The solution therefore would be to only add the amount (70 or 90) if the condition is true (i.e. replace += in if ((add += ... to + but leave the lines within the blocks the qay they are).

As a side note, an alternative way to break out of your loop in the else clause would be the break keyword.

P.s. You come from a Python background?

Traveller
  • 604
  • 6
  • 16
  • yes i do come from a python background, i check the if else statement to work with a fresh page refresh with 115 as the input so the first if statement shouldnt affect the if else i dont think, i added the HTML for clarification – Yagami_Light Aug 07 '22 at 02:21
  • @Yagami_Light even if the contents of the first if block are not executed, the if statement itself is. So `plates45 +=2` is not executed, but `(add += 90) <= lbs` is, and that has the side-effect of changing the value of `add`. – Traveller Aug 07 '22 at 02:25
  • 1
    wow thanks i tested it with alert(add) and ur right! i did not know if statements worked like that thanks – Yagami_Light Aug 07 '22 at 02:33