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

Ben Wilburn
Ben Wilburn
1,307 Points

I cant seem to figure this else if challenge out.

I have tried several different layouts of code but all it ever tells me is that there is a syntax error. I've gone back and look at the syntax in the video several times. I have even put the code into Visual Studio for it to point out my mistake and all is says is to put a ; in a place that Dave says not to put it (I did anyway & it created more problems). I don't see the error. Maybe a different set of eyes can point it out for me.

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

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

the > weren't in the original code.. its just something else I tried and watched fail. So, analyze the code minus the ">".

3 Answers

Tushar Singh
PLUS
Tushar Singh
Courses Plus Student 8,692 Points
var isAdmin = false;
var isStudent = false;

if ( isAdmin ) {
    alert('Welcome administrator');
} else if (isStudent) {
    alert('Welcome student');
} else {
  alert("Who are you?");
}
Rohit Tolawat
Rohit Tolawat
8,277 Points

Hi Ben, The issue with your code at first glance seems to be the following: After the final else clause, there should not be any conditional statement. The general structure of if.. else if.. else clause is: if (cond){} else if(cond){} else if(cond){} else {}

This seems to be violating here. Please try with the suggested solution and update if it works out well for you.

Thanks, Rohit.

Ben Wilburn
Ben Wilburn
1,307 Points

Thank you Tushar, I had been at it for a while and I couldn't remember all the rules. Your suggestion worked! Appreciate the help!