Modifying a variable in javascript? Not Working -
i'm trying make when pacman runs ghost loses life. , according number of lives has specific message displayed, variable doesnt seem taking account i'm changing it.
any suggestions?
var life=3; // lorsque l'on rencontre un fantome. function mancheperdue() { x_pacman = 13; y_pacman = 10; life = life-1; if (life=2) { window.alert("2 lives left"); } else if (life=1) { window.alert("1 life left"); } else { window.alert("game over"); gameover(); } }
you need use equality operator ==
in if's, not assignment operator =
.
if (life == 2) { alert("2 lives left"); else if (life == 1) { alert("1 life left"); } else { alert("game over"); gameover(); }
using =
means you're reassigning value in each check. example:
var lives = 3; if (lives = 1) { alert("1 life"); // show } alert(lives); // show "1"!
javascript has idea of strict equality using ===
operator. in case of strict equality, types must match.
1 == 1; // true 1 == "1"; // true 1 === "1"; // false
for it's worth, you'll notice don't need write window.alert
, writing alert
suffice because window
global scope.
Comments
Post a Comment