Unexpected Token else


#1

var isEven = function(number) {
if(number % 2);
return true;
}else{
return false;
}

Dont know what to do


#2

if(number % 2);

@codeace40947, There's a few problems with this if statement, so I'm going to show you what it should look like, then explain why it's supposed to look like that.

if (number % 2 === 0) {

Alright so if statements have to evaluate as true or false, I.E. if 2 minus 1 is equal to 1. Then you know that's correct right? Well that's what the computer is seeing, but what you have is like an incomplete question.

You're asking "if number modulo 2". and the computer is like. "if the number modulo 2 is what?" It's expecting a comparison. So you'll know if you've done your if statement correctly if you add a comparison operator:

< "Less Than"
> "Greater Than"
<= "Less Than or Equal To"
>= "Greater Than or Equal To"
<== "Less Than or Equal To"
>== "Greater Than or Equal To"
== "Equal To"
=== "Equal To"
!= "Not Equal To"
!== "Not Equal To"

All of these are comparison operators, they see if something is (insert comparison here) to another thing.

LASTLY!!!!
Never ever ever add a semi-colon after your if comparison parameters!

if(number % 2 === 0); {
                    ^

That will kill your code faster than I kill tacos, which is pretty fast!


#3

@lolman, That did not work i tryed it but it still said SyntaxError: Unexpected token else so i don't know what is going on with it"
New code
var isEven = function(number) {
if(number % 2 === 0)
return true;
} else {
return false;
}


#4

if(number % 2 === 0)
 return true; //<---
} else {

If you have a single line of code in your if statement, like you do there, then you don't need opening or closing brackets " { } ". However, You can't simply have a closing bracket " } " before your else statement, if you don't add an opening bracket " { " to your if statement.

So you can do one of two things.

1.

if(number % 2 === 0) {
                     ^
 return true;
} else {

Add an opening bracket there, or!

2.

if(number % 2 === 0) 
 return true;
  else {
^

Delete the closing bracket at the end there.

But it's best practice to always use an opening and closing bracket, this way you don't lose track of how many you're supposed to have in your code.


#5

@lolman Thanks for helping