Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
- Introducing Conditional Statements 8:39
- Comparison Operators 7:19
- Conditional Statements and Operators Review 7 questions
- Use Comparison Operators 1 objective
- Boolean Values 7:32
- Program Multiple Outcomes 5:45
- Booleans and Else If Review 5 questions
- Write an 'if' Statement 1 objective
- Add an 'else if' Clause 1 objective
- Add a Final 'else' Clause 1 objective
- Test Multiple Conditions With the && Operator 3:57
- Test Multiple Conditions With the || Operator 4:04
- Document Your Code with Comments 4:43
- Multiple Conditions and JavaScript Comments Review 6 questions
- The Conditional Challenge 2:17
- The Conditional Challenge Solution 6:23
- Use Multiple Conditions 1 objective

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Add variety to your programs using 'else if' statements. 'else if' provides multiple program paths with numerous conditions.
Improve the Random Number Guessing Game
Follow the notes below to put the else if
clause to use in the random number guessing game you wrote earlier. So far in the game, the player either guesses the correct answer or not. But you can improve it by giving the player a clue then letting them guess again.
let correctGuess = false;
const number = 6;
const guess = prompt('Guess a number between 1 and 10.');
if ( +guess === number ) {
correctGuess = true;
} else if ( +guess < number ) {
const guessMore = prompt(`Try again. The number is higher than ${guess}`);
if ( +guessMore === number ) {
correctGuess = true;
}
} else if ( +guess > number ) {
const guessLess = prompt(`Try again. The number is lower than ${guess}`);
if ( +guessLess === number ) {
correctGuess = true;
}
}
if ( correctGuess ) {
console.log("You guessed the number!");
} else {
console.log(`Sorry. The number was ${number}.`);
}
The code above performs three tests:
- Does the player's guess match the random number?
- Is the player's guess less than the random number?
- Is the player's guess greater than the random number?
else if Conditions
The two else if
clauses test two conditions:
- First, is the guess lower than the random number?
- Next, is the guess higher?
If the guess is lower, the program lets the player know and asks them to guess again via a second prompt()
dialog. The program then checks the second guess and tests if it's correct. This is done by nesting an if
conditional statement inside the code block.
The nested if
conditional only runs if the first else if
condition is true
-- if the player's second guess is less than the random number. If that guess is correct, the correctGuess
variable changes to true
.
The second else if
clause also has a nested if
statement. If the player's second guess is greater than the random number, correctGuess
changes to true
.
The rest of the code should remain the same. Now the user has a second chance to guess if their first guess is wrong!
Resources
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Jessica Perry
2,688 Points1 Answer
-
monique champagne
761 Points3 Answers
-
Devan Jeffner
Full Stack JavaScript Techdegree Student 770 Points1 Answer
-
Angelica Islas
4,082 Points1 Answer
-
Forrest Pollard
1,988 Points2 Answers
-
vikrant kaushal
Front End Web Development Techdegree Student 2,558 Points3 Answers
-
Daniel Statsenko
Full Stack JavaScript Techdegree Student 4,235 PointsThe very last else{} in the program yields a "Uncaught TypeError: Assignment to constant variable."?
1 Answer
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up