2

Hi to all I am trying to make a program in order to output when a number is Even using recursive calls. Can anybody please say me why it doesn't work as I expected?.

const isEven = num => {
  if (num === 0) return true;
  else if (num === 1) return false;
  //console.log(num);
  isEven(num-2);
}

isEven(16); // Epected Log: 0 but instead it returns undefined
1
  • 5
    You are not returning the return value of isEven(num-2). Commented Feb 20, 2020 at 13:11

2 Answers 2

3

Because for arguments different than 0 or 1 there is no return value. The last line:

  isEven(num-2);

should be

  return isEven(num-2);
Sign up to request clarification or add additional context in comments.

1 Comment

@phortela1n: Don't worry; it's a very common mistake. There are many, many questions in the "recursion" tag with that same issue.
1

You are not returning a value.

Change:

isEven(num-2);

To:

return isEven(num-2);

1 Comment

Thanks you Jeremy : )

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.