Finding Even/Odd Number Without Using Modulo

As the name suggests, I want to determine if the input is either even or odd. Here’s my code:

function newFunc (num) {
  var newValue = 0
  for (var i = num; i > 0; i -= 2) { //start at num, if num is greater than 0, decrement by 2
    if (num === 0 || num === 1) { // if num is either even or odd
      newValue += num // add that to newValue
      break; //stop: I don't want this function to continue since it'll beyond 0
  return newValue 


What’s wrong?

What are you trying to do exactly? Is there a reason for why you do not want to use Modulo to determine this? Because this is the fastest and easiest way to figure out if a number is even/odd.

Your loop bound should include 0 since you use the case when num===0 in your loop.

But… why? Just to see if you can? I would not do this in any production code, but it’s an interesting exercise just to see if you can do it.


I was in a coding bootcamp prep course, and I noted any questions I was stuck on in an Evernote notebook. I’m just using this as practice (that question in the course specificallyasked me not to use modulo).


Note that it is simplest to use bitwise operators if the challenge is to avoid modulo. You check by seeing if if the last bit is set (ie it’s a 1: if it is, it’s odd, if it’s not, it’s even):

function isOdd (n) {
  return Boolean(n & 1);

What exactly do you want your function returns? Because your function is returning a number. I think it should returns “The number 7 is odd”.
In the for loop, the variable that decrements is i
Your idea is not quite incorrect, but you need to understand first how the variables are changing. The for loop is decrementing i, so the values of i are 7, 5, 3, 1 in every step of the for loop. It should break before becoming negative, so you must ask i >= 0; So, the for loop breaks when i = 1 or i = 0. In the for statement you could asign newValue = i. Finaly, the if statement could say
if (newValue ==0) {

return "The number " + num + “is even”

} else {

return "The number " + num + “is odd”


Are you ever try bitwise operators this is also a easy way to check even or odd numbers, try bitwise & operator to check even or odd number (check result if it true odd else even) .

num & 1?  "odd number" :  "even number";

I want it to return either 1 (meaning odd) or 0 (meaning even), but I could definitely implement your idea saying “The number " + num + " is even”.

I’m not familiar with a bitwise & operator, could you explain that to me?

See @DanCouper’s reply. Also, I suggest you do a search on your own to read about bitwise operators, to better understand them.

I have to ask. Did you just look at the code and the final result to try and figure it out, or did you actually run it, either debugging or logging out variables to check what you were doing? Because I get the feeling you didn’t really debug the code, as opposed to just reading the code and looking at its final result.

I would suggest that you really have to debug code to check your assumptions. Your brain is not a code parser, you can easily make mistakes and never see (i.e. read) them. Just like you can with any written language. But if you look at the code as it is running (debugging or logging) and check your assumptions and values, it is a lot more apparent what is happening. Sort of like proofreading or getting text read back to you.


This is v relevant:

I think my favourite method:

Also the one that posts a question to Twitter asking if a number is even then waits for a response, but I think it’s pretend code, that one that asks Google works, allegedly

uurgh, and have to do this: for people who may misunderstand, as do some people in the thread, the answer is modulo (or last significant bit I guess), that’s the joke

1 Like

Bitwise operator access bit’s of a number mean In binary number system every number represent 0 or 1 and through bitwise operator we can access those bit and manipulate them.

ex: suppose we have a number < 7 > this number represent in decimal system.

If we want to write this number in binary we would write this number like this.

ex: 7 decimal = 0111 binary.

And in even or odd scenario & operator work on binary bits.

If you want to check number is even or odd, remember in binary system

(If the number is even the right most bit is always being 0 and in odd numbers the right most bit is always being 1)

< 011 (“1” this bit) >
In example binary number the right most bit is 1 so this is odd number.

further reference:
try to learn about binary number system and try your self.

1 Like