Javascript: recursive function returns undefined for existing value
<p>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.    </p>

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

<p>Where's my mistake? Here's the code to better illustrate:    </p>

<pre><code>    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" &lt;-- the returned value

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

// "key value is now: I" &lt;-- test log 
// undefined &lt;-- but the return value is undefined! why?!
</code></pre>