Dear all,
I have few questions regarding this code:
- Even though I know by deleting the else statement and add the
return undefined
before the last curly bracket is the right answer. I’m wondering why the else statement will not work here. - The code here:
for(let i=0; i<arr.length; i++){
num = arr[i]
if(func(num)){
return num
If there are more than one arr[i]
fulfill the requirement, won’t it return all of them?
If not, why?
Thanks for answering.
Full code
function findElement(arr, func) {
let num = 0;
for(let i=0; i<arr.length; i++){
num = arr[i]
if(func(num)){
return num
}else{
return undefined
}
}
}
findElement([1, 2, 3, 4], num => num % 2 === 0);
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Safari/605.1.15
Challenge: Finders Keepers
Link to the challenge: