Soo… wondering why, in this problem, the map function does not work, but the for loop does?
function findElement(arr, func) {
arr.map((num) => {
return func(num) ? num : undefined;
});
return undefined;
}
findElement([1, 3, 5, 8, 9, 10], num => num % 2 === 0);
i.e. Below works, but above does not.
function findElement(arr, func) {
let num = 0;
for(let i = 0, n = arr.length; i < n; i+=1)
num = arr[i];
if (func(num)) {
return num;
}
}
return undefined;
}
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/finders-keepers