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

Himanshu Geed
Himanshu Geed
2,175 Points

Not sure what's wrong with the code

It would be great if you can check this code out and suggest what am I doing wrong here.

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

if ( isAdmin ) {
    alert('Welcome administrator');
} else if (isStudent) {
    alert('Welcome student');
}
else (isStudent && isAdmin) {
  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

Hi Himanshu!

You have one small error. Else statements do not take conditions. So delete the parenthesis and everything inside it.

code:

var isAdmin = false;
var isStudent = false;

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

Hope that helps!

Thanks! :)

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Let's say for example that your system only has two types of users: students and admins. The last else is what happens when everything else fails. So our first if checks to see if it's an admin. If it's not, it goes on to the next possibility... a student. If that also fails, we want to know who they are. An else statement does not take a condition. Again, it is what happens when everything else before it has failed.

Removing the (isStudent && isAdmin) will cause the challenge to pass.

That being said, that condition would have checked to see if the person is both a student and an admin instead of being neither.

Hope this helps! :sparkles: