Why mt code isnt working


#1



the error message is: unexpected token else


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

Replace this line with your code.


#2

else is an optional part of an if-statement, you can't put anything between them because they're part of a single structure


#3

I don't understand what you mean by "you cant put anything between them because they're part of a single structure"? also i thought else is not optional of an if statement. isn't that part of the if/else code?


#4

an if-statement looks like this:

if (condition) statement else statement
Where the else part is optional

You've got a bunch of other stuff between the first statement and the else.


#5

You have a space between % and 2, try %2. And you are missing curly brackets after IF statement: if( ) { };
Hope that helps,


#6

this is my code now: var isEven = function(number) if (number %2
=== 0);{ return true;};
else {return false;};
isEven(2);
it says unexpected token: if


#7

this is my code now: var isEven = function(number) if (number %2
=== 0);{ return true;};
else {return false;};
isEven(2);
it says unexpected token: if
why is that happening?


#8

You need a set of {} around the entire function itself. So after the "function(number)" and right before the "if statement", put the opening curly brace. And then at the very end of your code put an ending curly brace.


#9

Why are you putting semicolons after if conditions and before an else?


#10

Please use the right format to display the code the way you have it on your first question at the top of the page then add the proper spacing, it's really confusing to read.


#11

What is the error in below code:

// Declare your variables here!

var hungry = true;
var foodHere = true;

var eat = function() {
// Add your if/else statement here!

if(hungry && foodHere) {
// if hungry AND foodHere:
console.log("Both are true");
} else if(!(hungry || foodHere)) {
// if NOT hungry AND foodHere:
console.log("Let's see if we can change your mind.");
} else {
console.log("You only like one but not the other? We'll work on it.");
}
};

eat();

result is being displayed as below:

Awesome! Let's keep learning!
You only like one but not the other? We'll work on it.


#12

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.