jeudi 30 juillet 2020

else if condition not utilised properly with other if statements -JS

l have an issue with a else if or if statement in my code, whereby it does not do it's intended function. The intended function is that it should display "Ran out of lives" and then end. l have tried different ways of sorting the if statement but with no success, it always 'skipped it'.

This is a snippet of my code.

var now = new Date();

now.setSeconds(now.getSeconds() + 30);
alert(now)
for (lives = 0; lives < 3; lives++) {

  var guess = prompt("_ _ _");

  var currenttime = new Date();

  if (guess.toUpperCase() === "BOO" && currenttime < now) {
    alert("Right");
    break;
  } else if (guess.toUpperCase() === "HINT" && currenttime < now) {
    alert("Online or offline entertainment.");
  } else if (currenttime >= now) {
    alert("now ended \nGame over");
    lives = 3;
    break;
  } else if (lives >= 3) {
    alert("Ran out of lives")
  } else {
    alert("Wrong");
  }
}

Aucun commentaire:

Enregistrer un commentaire