Home > Software design >  Javascript simple if/else statement Quiz Game with 5 questions that outputs results at the end
Javascript simple if/else statement Quiz Game with 5 questions that outputs results at the end

Time:03-22

Anyone care to help me get this code running correctly? Just simple if/else statements Javascript. I have tried attaching a "do {" before each of the next questions, but I couldn't get that to work. It keeps saying that "Uncaught SyntaxError: Unexpected end of input".

alert("Welcome to the 'Guess that Celebrities Age' Game! Let's see how many out of 5 you 
can guess right.")
let guess = 0;
let guess1 = 0;
let guess2 = 0;
let guess3 = 0;
let guess4 = 0;
let guess5 = 0;
let winsCounter = 0;
let losesCounter = 0;
do {
  guess = prompt("How old is Cher?");
  if (guess == 75) {
  alert("Correct! Cher is 75 years old. You guessed the number in tries.");
    winsCounter  ;
}
  else {
    alert("Wrong! Sorry, better luck next time.");
    losesCounter  ;
  }


 guess2 = prompt("How old is Elton John?");
  if (guess2 == 74) {
  alert("Correct! Elton John is 74 years old. You guessed the number in tries.");
    winsCounter  ;
}
  else {
    alert("Wrong! Sorry, better luck next time.");
    losesCounter  ;
  }


  guess3 = prompt("How old is Doja Cat?");
  if (guess3 == 26) {
  alert("Correct! Doja Cat is 26 years old. You guessed the number in tries.");
    winsCounter  ;
}
  else {
    alert("Wrong! Sorry, better luck next time.");
    losesCounter  ;
  }


  guess4 = prompt("How old is Christina Ricci?");
  if (guess4 == 42) {
  alert("Correct! Christina Ricci is 42 years old. You guessed the number in tries.");
    winsCounter  ;
}
  else {
    alert("Wrong! Sorry, better luck next time.");
    losesCounter  ;
  }


  guess5 = prompt("How old is Seth Rogan?");
  if (guess5 == 39) {
  alert("Correct! Seth Rogan is 39 years old. You guessed the number in tries.");
    winsCounter  ;
}
  else {
   alert("Wrong! Sorry, better luck next time.");
    losesCounter  ;
  }
}

CodePudding user response:

Your do statement is incorrect. It needs the while keyword at the end.

So, you can change your code to the template below to get it working.

let i = 0;
do {
  console.log(i);
  i  ;
} while (i < 5)

Basically, after the do keyword, you can add your code in the curly braces.

However, at the end of the curly braces (}), you can then add the while keyword, and a boolean condition in the brackets (parentheses).


Edit: Here is the documentation for do...while.

  • Related