I am trying to return the first value which is greater than 25 from an array, without using other methods such as filter. Assume the array is always sorted.
The function always returns undefined. Why is that happening? Here is the code:
function checkArr(arr){
arr.forEach(i => {
if (i>25) return i;
})
}
console.log(checkArr([10,20,34,45]))
The output should be 34.
undefinedis the returned value in aforEachloop: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. I'd be amazed if it returned something else...arr.forEachwhich is whycheckArris returningundefined. There are other ways to do what you want and I'm sure the answers will cover that.const checkArr = arr => arr.find(i => i > 25)