Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSHIMRON LAROSE
Courses Plus Student 733 PointsIt ask you to put an else statement to make an alert pop up saying who are you? what do you put in the else ()?
to have the alert pop up saying who are you?
var isAdmin = false;
var isStudent = false;
if ( isAdmin ) {
alert('Welcome administrator');
} else if (isStudent) {
alert('Welcome student');
}
else () {
alert("Who are you?");
}
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JavaScript Basics</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
2 Answers
Steven Parker
231,271 PointsA final "else" does not take a conditional expression.
It doesn't need one, since it handles anything not already covered by the rest of the "if" and "else if" statements.
Juan Luna Ramirez
9,038 PointsYou can read the conditional statement like this: "If the user is an Administrator then say "Welcome administrator", otherwise if the user is a Student then say "Welcome Student", otherwise if all else fails then say "Who are you?"
Notice how the last part is different because we are not checking for anything in particular. We are just saying that if all of the previous conditional expressions don't pass then we need to perform this default action, in this case alert "Who are you?"
Because you are not asking to check for anything in particular you don't need a conditional expression.
So you simply write:
if (condition1) {
block of code to be executed if condition1 is true
} else if (condition2) {
block of code to be executed if the condition1 is false and condition2 is true
} else {
block of code to be executed if the condition1 is false and condition2 is false
}
SHIMRON LAROSE
Courses Plus Student 733 PointsSteve I don't see how's that possible without including the parenthesis in the alert action to make it say who are you. Also Juan and Steve I tried every scenario I can think of to make this happen I keep getting it wrong what's the correct format?
Steven Parker
231,271 PointsYou're right that he parentheses after alert are important.
The ones you don't want are the ones after the last "else".
Juan Luna Ramirez
9,038 PointsThe format is the pseudo code written by Steven and myself. Compare your code to the pseudo code. What is the difference?
Here is the pseudo code in the correct "format" written in a way that resembles the way you used whitespace in your code:
if ( condition1 ) {
// block of code to be executed if condition1 is true
} else if ( condition2 ) {
// block of code to be executed if the condition1 is false and condition2 is true
}
else {
// block of code to be executed if the condition1 is false and condition2 is false
}
SHIMRON LAROSE
Courses Plus Student 733 PointsSHIMRON LAROSE
Courses Plus Student 733 Pointsi'm not following exactly what you mean still confuse?
Steven Parker
231,271 PointsSteven Parker
231,271 PointsPerhaps this pseudo-code will help illustrate my point: