It works with for loop , then what is the problem with forEach?
function findElement(arr, func) {
arr.forEach(x => {
if (func(x))
return x;
});
return undefined;
}
findElement([1, 3, 5, 8, 9, 10], num => num % 2 === 0);
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36
Challenge: Finders Keepers
Link to the challenge:
ILM
August 30, 2021, 10:21am
2
KaranPato:
return undefined;
this is the only return statement in your findElement
function, also forEach
returns undefined
on its own so you can’t even just return the forEach
function
So could there be a solution with forEach?
ILM
August 30, 2021, 12:07pm
4
yes, forEach
can be used, but if you want to use an array method, you can do some research and find a more approrpiate method
Alright, Thank you. I will find some more appropriate and optimal methods.
Let me know when you see this once.
system
Closed
March 1, 2022, 1:01am
7
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.