mercredi 9 septembre 2020

Javascript conditional statement not working as intended

I made use of the conditional statement below in Python and it work well, then while practicing JavaScript mins ago, it doesn't work.

var pos = 0
setInterval(move, 50)
var box = document.getElementById("box");


function move() {
  var dec = "right"
  
  if (dec == "right") {
    pos += 5
    box.style.left = pos + "PX"
   
    if (pos == 150) {
      dec = "left"
      console.log(dec) //dec is "left"
    }
  } else {
    pos -= 5
    box.style.left = pos + "PX"
   
    if (pos == 0) {
      dec = "right"
    }
  }
}

I expect the else to be evaluated when dec == 'left' (box to move to the left, but it doesn't).

Aucun commentaire:

Enregistrer un commentaire