Javascript: recursive function returns undefined for existing value

ingo

I am trying to loop an array using a recursive function. The loop should stop and return the value of the key, if it matches a given regex pattern.

The loop stops correctly when the condition is met. However, it only returns the key's value if the match occurs for the first key (index 0) in the array and returns 'undefined' for the rest.

Where's my mistake? Here's the code to better illustrate:

    function loop(arr,i) {
  var i = i||0;
  if (/i/gim.test(arr[i])){
    console.log("key value is now: " + arr[i])
    return arr[i]; // return key value
  }
  // test key value
  console.log("key value: " + arr[i]); 

  // update index
  i+=1; 

  // recall with updated index
  loop(arr,i); 
}

console.log( loop(["I","am", "lost"]) ); 
// "key value is now: I"
// "I" <-- the returned value

console.log(  loop(["am", "I", "lost"])  ); 
// "key value: am" 

// "key value is now: I" <-- test log 
// undefined <-- but the return value is undefined! why?!
Rajaprabhu Aravindasamy

You have to return the value from the recursive call,

  // recall with updated index
  return loop(arr,i); 
}

The final call for the function loop will return a value, but the other calls for the same function returns undefined. So finally you end up in getting undefined

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Recursive function in Javascript returns undefined

Why this recursive javascript function returns undefined?

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

Simple Recursive Javascript Function Returns Undefined

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

TypeScript Recursive function returns undefined

Javascript function returns previous value or undefined

Function returns 'undefined' value

Recursive function returning undefined value

Javascript function returns undefined

Javascript function returns undefined?

recursive function returning undefined javascript

Recursive function on sorted array returns undefined

Recursive function to add noise to a polygon returns undefined

Submitting an existing form with javascript returns undefined

Function return value returns as undefined

Async Javascript function returns undefined

Javascript function works but returns undefined

Javascript function returns undefined, why?

Javascript "this" value is undefined in function

Javascript returns value function

Undefined return value mongoose / nodejs recursive function

Python recursive function returns None instead of value

Javascript return undefined of recursive function with Date()

Recursive searching returns undefined

Recursive additive function from one cell value to an existing cell value

Recursive function returns undefined regardless of enough return statements

Function returns undefined though it has value

Why my debounce function returns undefined value?