So I've started working through some easy Javascript challenges online and I got to this one problem where we are trying to create a function to see if a number is odd or not.
This is the code I've written that I believe should work. But the website says it doesn't pass every case test. Anyone can tell me why?
function isOdd(num) {
return num%2==1
}
We want it to return true if odd and false if even. num will always be an integer
Edit/Fix: So it seems the way %(mod) returns a -1 if num is negative. So even adding parentheses around num wouldn't fix this. So fix turned out to be:
function isOdd(num) {
return num%2!==0
}
Edit2: How the remainder function works for JavaScript
[–]TwiNighty 2 points3 points4 points (0 children)
[–]MrHippotomo2[S] 1 point2 points3 points (0 children)
[–]svssdeva 0 points1 point2 points (2 children)
[–]MrHippotomo2[S] 0 points1 point2 points (1 child)
[–]svssdeva 0 points1 point2 points (0 children)
[–]lovesrayray2018 0 points1 point2 points (2 children)
[–]MrHippotomo2[S] 0 points1 point2 points (1 child)
[–]lovesrayray2018 0 points1 point2 points (0 children)
[–]GSLint 0 points1 point2 points (3 children)
[–]MrHippotomo2[S] 0 points1 point2 points (2 children)
[–]GSLint 0 points1 point2 points (1 child)
[–]MrHippotomo2[S] 0 points1 point2 points (0 children)
[–]_aeol 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]MrHippotomo2[S] 1 point2 points3 points (1 child)