Nested IF Statements and Algorithms
This tutorial is part of a web programming course for people who have no programming experience.
Nested IF Statements
Nested IF Statements - Here are the requirements for our program:
-
If the user likes books, ask them if they like Sci Fi books:
- If the user likes Sci Fi books, then recommend Frankenstein.
- If the user does NOT like Sci Fi books, then recommend Gone With the Wind.
-
If the user does NOT like books, then ask them if they like movies.
- If the user likes movies, then recommend the movie Social Networking.
- If the user does NOT like movies, then recommend that they take up knitting.
Here's how we can write code for the algorithm:
var likesBooks = prompt("Do you like books (enter 'yes' or 'no')?");
if(likesBooks == "yes"){
// the user likes books...
var likesSciFi = prompt("Do you like Sci Fi books (enter 'yes' or 'no')?");
if(likesSciFi == "yes"){
alert("I recommend Frankenstein!");
}else{
alert("I recommend Gone With the Wind!");
}
}else if(likesBooks == "no"){
// the user does NOT like books...
var likesMovies = prompt("Do you like movies (enter 'yes' or 'no')?");
if(likesMovies == "yes"){
alert("I recommend Social Networking!");
}else{
alert("I recommend knitting!");
}
}else{
alert("It was a yes/no question! And remember that JS is case sensitive!");
}
MAKE SURE TO TEST ALL POSSIBLE CODE BRANCHES