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 trialDaniel Jaquez
2,430 Pointsi need help withConditional statement question challenge please
Add a final else clause to this conditional statement so that if the isAdmin variable and isStudent variables are both false an alert opens with the message "Who are you?"
var isAdmin = false;
var isStudent = false;
if ( isAdmin ) {
alert('Welcome administrator');
} else if (isStudent) {
alert('Welcome student');
}
else (false){
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>
3 Answers
Karan Nahar
16,157 PointsYou did good. But look at your else statement. else does not take a argument. Here only if statement tests for a condition and if it is false else block runs.
var isAdmin = false;
var isStudent = false;
if ( isAdmin ) {
alert('Welcome administrator');
} else if (isStudent) {
alert('Welcome student');
} else {
alert('Who are you?');
}
If both isAdmin and isStudent are false we receive an alert Who are you?
Sean T. Unwin
28,690 PointsRemove the (false)
from the final else
statement. There is no bracketing for an else
clause.
Johannes Scribante
19,175 PointsHi Daniel,
An if
statement can be used in three ways:
-
as a if on its own
if (true) { alert('condition is true' ); }
-
as an if with an else
var condition = false if (condition) { alert('condition is true' ); } else { alert('condition is false' ); }
in this case if the first condition in the first if statement is not met the block of code after the else will be executed not matter what the condition is, as long as it is not true.
-
as an if with an else if and an else
var condition = 3 if (condition === 1) { alert('condition is 1' ); } else if (condition === 2) { alert('condition is 2' ); } else { alert('condition is neither 1 or 2' ); }
Hopefully you can follow the trend, so basically this is very similar to the second version, however is neither the first or the second conditions are met the final else block of code will be executed. Based on these you will also notice that the final else does not take a condition to test for, as if the conditions (see the if there lol) are not met prior to the else the else will be executed.
Hope this helps
Daniel Jaquez
2,430 PointsThanks, Jonathan that helps a lot
Daniel Jaquez
2,430 PointsDaniel Jaquez
2,430 PointsAwesome thank you so much i was struggling to understand but this puts it in perspective