Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics Making Decisions in Your Code with Conditional Statements Program Multiple Outcomes

Forrest Pollard
Forrest Pollard
1,988 Points

What is wrong with this code?

i thought i did a good job by trying to improve the number game by writing different code from the one shown in the notes of this video. here is my code:

let correctGuess = false;
const number = 6;
var guess = prompt("Guess a number between 1 & 10 to win a prize!");

if ( +guess === number ) {
 correctGuess = true 
} else if ( +guess > number ) {
  alert(`Incorrect, The number is lower than ${guess}. Guess again`);
  var newGuess = prompt("LAST CHANCE OR YOU FAAAAAAIL");
if ( +guess < number ) {
  alert(`Incorrect, the number is greater than ${guess}. Guess again`);
  var newGuess = prompt("LAST CHANCE OR YOU FAAAAAAIL");
}
}

if ( correctGuess ) {
 alert("You win, GG2EZ")
} else if ( +newGuess === number ) {
 alert("You win, GG2EZ"); 
if ( +newGuess != number ) {
 alert(`You have performed a critical fail! The number was ${number}`); 
}
}

however when I run it, it says there is an "Unexpected token: { at line 10" it is the function bracket for the end of the first if-else block. I am unsure why

EDIT; i have fixed the unexpected token problem by changing how the if-else was structured with the function brackets, but i dont fully understand why it works so would like an answer to that and will leave my question up, because even though it works im not sure why lol. Also, when i run this code, the only if-else thing that works currently is if you input a higher number than 6. This code challenge is kicking my butt

let correctGuess = false;
const number = 6;
let guess = +prompt("Guess a number between 1 & 10 to win a prize!");
let newGuess;

if ( guess === number ) {
 correctGuess = true; 
} 
else { 
  if ( guess > number ) {
    alert(`Incorrect, The number is lower than ${guess}. Guess again`);
  }

  if ( guess < number ) {
    alert(`Incorrect, the number is greater than ${guess}. Guess again`);
  }

  newGuess = +prompt("LAST CHANCE OR YOU FAAAAAAIL");
}

if ( correctGuess ) {
  alert("You win, GG2EZ");
} 
else if ( newGuess === number ) {
  alert("You win, GG2EZ"); 
}

if ( newGuess !== number ) {
  alert(`You have performed a critical fail! The number was ${number}`); 
}

You are missing a few semicolons, and the end of the code you open a "else if " statement, but you have nested the final if statement inside it so the ending message wont ever execute.

Compare with fixed code here and see if you can get it to work :)

let correctGuess = false;
const number = 6;
var guess = prompt("Guess a number between 1 & 10 to win a prize!");

if (+guess === number) {
  correctGuess = true;
} 
else if (+guess > number) {
  alert(`Incorrect, The number is lower than ${guess}. Guess again`);
  var newGuess = prompt("LAST CHANCE OR YOU FAAAAAAIL");
}
if (+guess < number) {
  alert(`Incorrect, the number is greater than ${guess}. Guess again`);
  var newGuess = prompt("LAST CHANCE OR YOU FAAAAAAIL");
}

if (correctGuess) {
  alert("You win, GG2EZ");
}
else if (+newGuess === number) {
  alert("You win, GG2EZ");
}
  if (+newGuess != number) {
alert(`You have performed a critical fail! The number was ${number}`);
  }

I fixed up up your code so it doesn't have that "you performed a critical fail" at the end, by fixing the if statement at the end

if ( newGuess !== number ) {
  alert(`You have performed a critical fail! The number was ${number}`); 
}

to this

let correctGuess = false;
const number = 6;
let guess = prompt("Guess a number between 1 & 10 to win a prize!");
let newGuess;


if ( +guess === number ) {
 correctGuess = true; 
} 
else  if ( +guess > number ) {
    alert(`Incorrect, The number is lower than ${guess}. Guess again`);
   newGuess = +prompt("LAST CHANCE OR YOU FAAAAAAIL");
  }
 else if ( +guess < number ) {
    alert(`Incorrect, the number is greater than ${guess}. Guess again`);
    newGuess = +prompt("LAST CHANCE OR YOU FAAAAAAIL");
  } 

if ( correctGuess ) {
  alert("You win, GG2EZ");
} 
else if ( newGuess === number ) {
  alert("You win, GG2EZ"); 
} else {
  alert(`You have performed a critical fail! The number was ${number}`); 
}

There's no need to write another if statement because the else will automatically pick it if it's false. I hoped this helped.