vendredi 26 avril 2019

A simple counter gives a different outcome in javascript compared to python

I'm trying to learn javascript. I'm having problems with a simple counter that gives a wrong outcome.

var x = 0

while (x < 10) {
  x++
  console.log("x:", x)

  if (5 < x < 10) {
    console.log("x is between 5 and 10")
  } else {
    console.log("getting out of the loop")
    break
  }
}

outcome

'x:'1
'x is between 5 and 10'
'x:'2
'x is between 5 and 10'
'x:'3
'x is between 5 and 10'
'x:'4
'x is between 5 and 10'
'x:'5

etc.

Whereas if I do the same with python, the result is correct

x = 0

while (x < 18):
    x+=1
    print("x:", x)

    if 5 < x < 10:
        print("x is between 5 and 10")

    else:
        print("getting out of the loop")
        break

outcome

x: 1
getting out of the loop

Process finished with exit code 0

Aucun commentaire:

Enregistrer un commentaire