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