If/else STUCK!


#1


I don't get it. I checked online and I can't see what I'm doing wrong. Yes, I read the syntax error but I can't figure out how to write it... would love som help!
Thanks!


SyntaxError: expected expression, got '==='


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


#2

Hey there.

The purpose is to determine whether num is even or not and return true or false respectively.

In the if statement condition we need to check num modulo 2 or num % 2 and compare (===) the result with 0.
Remember, Modulo finds the remaining amount from the divisible. If our number is divisible by 2 then we know we will have 0 remaining after the Modulo operation, and therefore will return true.

Hope that clears it up.


#3

Hi!

I'm sorry, I feel so stupid right now. Haha.

I get the concept (I think), I just wonder what to write?
Clearly if(num % === 2) isn't correct. How do i compare them? What do I miss?

Thank you!


#4

Don't feel stupid! These concepts are confusing for most people when they first learn about them. Including me.

Here's what you need to do.

var isEven = function(num) {
  // Directly below this comment is the line that changed from - > if(num % === 2) {
  if(num % 2 === 0) {
      return true;
  } else {
      return false;
  }
  
};
isEven(3);

So when we pass 3 to isEven() we will essentially be doing

num % 2 This is the same as saying - -> 3 / 2 = 1 (with 1 left over)

num % 2 === 0 now becomes - -> 1 === 0 Left over value from previous step is put forth to be compared

if(1 === 0) - -> evaluates to false. Therefore we skip the first block in the if statement and return the else result. Which returns false. isEven(3) is not an even number.

Hope that clears it up.


#5

Thanks!

You really helped!
Have a nice day :slight_smile:


#6

I get modulo just fine, but I get an error that says that I haven't defined the function named isEven. I looked up how to define functions and there seems to be nothing wrong with this compared to the examples that I saw, but I still get the error message.

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


#7

You need to take a second look at your if else condition.

Remember that = is the assignment operator, and === is the equality operator.

Also, it's good practice to end each statement within the if else code blocks with a semicolon (;) :

if (condition) {
  code block;
} else {
  code block;
}

#8

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