Simple Recursive Javascript Function Returns Undefined

Jules Manson

For an assignment I am supposed to write a recursive function that checks any integer for even or odd using N-2. If even returns true else returns false. But it returns undefined whenever a value is large enough to call itself. Please help!

function isEven(num) {
  console.log("top of function num = " + num);// For Debugging
  if (num == 0){
      return true;
  }
  else if (num == 1){
      return false;
  }
  else {
    num -= 2;
    console.log("num = " + num);
    isEven(num);
  }
}
console.log(isEven(0));
// → true
console.log(isEven(1));
// → false
console.log(isEven(8));
// → ??

Console Log Results:

top of function num = 0

true

top of function num = 1

false

top of function num = 8

num = 6

top of function num = 6

num = 4

top of function num = 4

num = 2

top of function num = 2

num = 0

top of function num = 0

undefined
Robba

You have forgotten the return statement before the recursive isEven(num) call.

See the snippet below:

function isEven(num) {
  //console.log("top of function num = " + num);// For Debugging
  if (num == 0){
      return true;
  }
  else if (num == 1){
      return false;
  }
  else {
    num -= 2;
    return isEven(num);
  }
}
console.log('0 is even: ', isEven(0));
// → true
console.log('1 is even: ', isEven(1));
// → false
console.log('8 is even: ', isEven(8));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Javascript, a simple recursive function

JavaScript typeof function passed as an argument returns undefined

My Elixir recursive function returns a list of lists, not a simple list

Javascript: recursive function returns undefined for existing value

Why this recursive javascript function returns undefined?

Async Javascript function returns undefined

TypeScript Recursive function returns undefined

JavaScript: Recursive function with Promise, resolve returns "undefined"

Javascript | Recursive function I created returns undefined along with the proper answer

Recursive function to add noise to a polygon returns undefined

Exporting a function using Javascript returns undefined

JavaScript constructor function returns values as undefined

JavaScript Function call returns undefined, why?

Simple javascript function return an undefined string

Recursive function returns undefined regardless of enough return statements

Promise inside the if() function returns undefined in Javascript

Javascript function returns undefined, why?

Javascript call function dynamically - returns undefined

Javascript function works but returns undefined

Javascript function returns undefined?

Javascript JSON parse function returns undefined

Javascript function returns undefined

Recursive function in Javascript returns undefined

Javascript return undefined of recursive function with Date()

recursive function returning undefined javascript

Javascript function returns previous value or undefined

Recursive searching returns undefined

Recursive function on sorted array returns undefined

Javascript Function return true/false returns undefined