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 trial

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements Add a Final Else Clause

SHIMRON LAROSE
PLUS
SHIMRON LAROSE
Courses Plus Student 733 Points

It 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?

script.js
var isAdmin = false;
var isStudent = false;

if ( isAdmin ) {
    alert('Welcome administrator');
} else if (isStudent) {
    alert('Welcome student');
}

else () {
 alert("Who are you?"); 

}
index.html
<!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
Steven Parker
231,269 Points

A 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.

SHIMRON LAROSE
SHIMRON LAROSE
Courses Plus Student 733 Points

i'm not following exactly what you mean still confuse?

Steven Parker
Steven Parker
231,269 Points

Perhaps this pseudo-code will help illustrate my point:

if (condition) { /* do stuff */ }
else if (condition) { /* do stuff */ }
else { /* do stuff */ }    //  <-- note: no condition!  (and no parentheses after "else")
Juan Luna Ramirez
Juan Luna Ramirez
9,038 Points

You 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
SHIMRON LAROSE
Courses Plus Student 733 Points

Steve 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
Steven Parker
231,269 Points

You'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
Juan Luna Ramirez
9,038 Points

The 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
}