Modulo in if/else if/else


#1


So I was able to get the code to run properly, and I just have a question.
Did it work because my "var submit = number;" pulled the number designated from my isEven function?


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

isEven(4);


More on Control Flow in JS - 11 - And
#2

Your solution passed because 4%2 === 0. If you gave it a positive odd number it would return false unless that number is 1.

What happens when you call 'isEven(1)'?

Your last 'else' check is funky. What number would fall through the previous if..else's to reach the last 'else'?


#3

You were able to get it to run? Just tried it and I didn't...

Syntax-wise, there are things you can improve:

Line 2 : var submit = number;
although nothing stops you from doing this, it's unnecessary. You can get to the point straight away:

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

If I'm not mistaken, this is the exercise where you should be using the isNaN function? Let's use it:

} else if (isNaN(number)) {
  return "a string saying that it's not a number";
}

Then, you wrote the else statement like this:

 } else { (submit%3);
   return false}
};

and this is why I'm surprised that you got it to work without errors. The syntax is wrong here.

The structure for an else should be:

} else {
  return false;
}

I'm really puzzled as to how you got this to work, and I'm thinking that I might have the wrong exercise here... Regardless, my post is merely an attempt to guide you towards a better syntax and better practice.

Javascript is a terrible language in that it's very lenient sometimes, and will allow things that shouldn't work (and wouldn't in other languages). Try to get the syntax right and you should be just fine though.

Keep it up and happy learning!


More on Control Flow in JS - 11 - And
#4

Thank you for your response,

This was for the first part of the lesson, 2 part brings in "NaN".
Feed back on proper syntax is always well appreciated, thank you once again.

I changed the code to look like
var isEven = function(number) {
//var submit = number;
if (number%2 === 0) {
return true;
} else if (number%2!==1){
return false;
} else { number >= 1;
return false}
};

isEven(10);

I will continue moving forward and asking questions.


#5

Try testing your changes with an odd number. What is the result?


#6

May I suggest reviewing your syntax once more, as the else part of the statement still isn't quite right :wink:

} else { number >= 1;
return false}
};

once you have written } else { everything that comes after the last curly bracket is a statement that acts depending on the condition. In this case, number >= 1 isn't part of any condition, it doesn't do anything, and shouldn't be there. Also, the curly bracket after false ends the else { }, making the closing }; useless and wrong within the code.

What you could have written, if you wanted it to be another condition, is:

} else if (number >= 1) {
  return false;
}

A condition always goes between brackets ( ), after an if or else if.

An else will always be

} else {
  // do something //
}

Feel free to ask should you have more questions regarding this


#7

So I see how the previous code makes absolutely no sense, and did not function.

I created

var isEven = function(number) {
//var submit = number;
if (number%2 === 0) {
return true;
} else if (number === 7) {
return false;
} else (number%2 === 1);{
return ("you did something right");
}
};

isEven(32);

Thank you for clarifying if/else condition clarifying.
Do I always clarify () - condition before { ?
Also,I imagine the next exercise will help make sense of the } else { portion for me. (Using NaN?)
I was struggling to make it make sense, with going with (number===7) it allowed the code to run properly. At least I believe so, when I swap between Even/Odd #'s I was receiving True/ my String I created. when I put isEven(7); I get "False" as well.

Thank you so much for all the help, I look forward to your response & continuing.


#8

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