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 trialMark Reitz
692 PointsI am getting a parse error code for the following code.
if else workshop. I using the following code.
var isAdmin = false; var isStudent = false;
if ( isAdmin = true ) {
alert("who are you?");
} else if (isStudent = true) {
alert("who are you?");
} else if (isAdmin = false && isStudent = false) {
alert("who are you?");
}
var isAdmin = false;
var isStudent = false;
if ( isAdmin = true ) {
alert("who are you?");
} else if (isStudent = true) {
alert("who are you?");
} else if (isAdmin = false && isStudent = 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>
2 Answers
Steven Parker
231,275 PointsIt's important to use the correct operator symbols. Be careful of the difference between the equality comparison operator ("==") and the assignment operator ("=").
And the final clause should be a plain "else" with no "if" or conditional expression. You don't need one since it will handle all the cases not already covered by the previous "if" and "else if".
Also, you never need to compare a boolean value with "true". Since it represents "true" or "false" by itself, just naming it does the job.
Mark Reitz
692 PointsThank you for your response. I understand the three things you addressed but when I apply them I am missing it.
1) Did I use comparison operators? I thought I was using assignment operators (and if so is that correct)? 2) I have tried the else clause and am getting the message ("Bummer: Hmm. I don't see the message 'Who are you?' in the alert message") 3) I am not clear about what you mean with the boolean...did I do something in my code incorrect? Below is my code.
var isAdmin = false; var isStudent = false;
if ( isAdmin = true) {
alert("who are you?");
} else if (isStudent = true) {
alert("who are you?");
} else {
alert("who are you?");
}
Steven Parker
231,275 PointsYou are using assignment operators, and that's the problem. You don't want to change the values, just test them.
A variable that holds "true" and "false" is called boolean. And since that's what these are, you don't need any kind of operator to test them. For example:
if (isAdmin) { // this is all you need to check if a boolean is true