-5

I was trying to call another function inside function iseven(n):

function iseven(n) {
    function remainder(n) {
        if (n%2==0) {
            return true;
        } else {
            return false;
        }
    }
}
console.log(iseven(4));

It returns undefined.

a right way to do this:

function a(x) {    // <-- function
  function b(y) { // <-- inner function
    return x + y; // <-- use variables from outer scope
  }
  return b;       // <-- you can even return a function.
}
console.log(a(3)(4));

nested functions JS

Nahu
  • 59
  • 9

3 Answers3

2

Try

function iseven(n) { return n % 2 === 0; }

Simon Franzen
  • 2,628
  • 25
  • 34
2

You want something more like this

function iseven(n)  { 
    if (n%2==0) { 
        return true; } 
    else { 
        return false; 
    } 
}

console.log(iseven(4));

And something a bit more succinct:

function isEven(n) {
   return n % 2 === 0;
}

Not quite sure why the original was structure that way..

Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
  • 1
    i would prefer the [strict equality operator](https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons) but that's more of a style thing in the case of your example – Rico Kahler Mar 08 '18 at 19:45
1

Your Main function iseven() does not return anything. Based on your code it should return false. Here's the fix:

function iseven(n) { 
    function remainder(n) { 
        if (n%2==0) { 
            return true; 
        } 
        else { 
            return false; 
        } 
    }
    //iseven() should return something
    return false; 
}
console.log(iseven(4));
centrinok
  • 300
  • 2
  • 11