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

Add a final clause 101 objective

I first tried linking them with a + sign and went along till i tried this. Any ideas?

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

if ( isAdmin ) {
    alert('Welcome administrator');
} else if (isStudent) {
    alert('Welcome student');
} else (isAdmin = false ) {
    alert("Who are you?");
} else (isStudent) {
    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

Alex Forseth
Alex Forseth
8,017 Points

Kaleb,

Here is the correct code. var isAdmin = false; var isStudent = false;

if ( isAdmin ) { alert('Welcome administrator'); } else if (isStudent) { alert('Welcome student'); } else { alert('Who are you?')

}
Maxime Duhamel
Maxime Duhamel
7,169 Points

Hi Kaleb,

To start with, the structure of an if else if statement can only contain 1 if and 1 else. In between as many else if as you want. The logic will try each steps and execute only if it is true.

if (true or false) { if it was true do this (if not got to the next else if check); } else if (true or false) { if it was true do this (if not got to the next else if check); } else { if not of the above were true do this; }

So in this example we check if 1: are you admin? 2: are you student? 3: if you not admin nor student ask who we are.

I hope it will make sens and you'll get the logic instead of having a direct answer to the challenge :)

Max