-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

4
  • 6
    You have a function in a function. The outer one does not return anything. What did you want to do with 2 functions? Commented Mar 8, 2018 at 19:37
  • Because you have a function in a function Commented Mar 8, 2018 at 19:38
  • Remove function remainder(n){ and one closing } Commented Mar 8, 2018 at 19:39
  • @nahu have you solved your problem ? Commented Mar 12, 2018 at 9:27

3 Answers 3

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..

Sign up to request clarification or add additional context in comments.

1 Comment

i would prefer the strict equality operator but that's more of a style thing in the case of your example
2

Try

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

3 Comments

if you expect it to return a boolean, i would do function iseven(n) { return n % 2 === 0; } but that's just being picky cause 0 and 1 that your functions returns are falsy and truthy values respectively
You are right @RicoKahler ! I will updated my answer
@nahu did my answer help you ?
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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.