Hey guys,
I’m learning this solution code and don’t understand what stops the for loop from returning more than just the first num making func true (Quote from instructions: “returns the first element in it that passes a ‘truth test’.”).
Won’t the loop keep going till i == arr.length and return every num that answers the condition func(num) == true?
Thanks in advance
the solution code:
function findElement(arr, func) {
let num = 0;
for (var i = 0; i < arr.length; i++) {
num = arr[i];
if (func(num)) {
return num;
}
}
return undefined;
}
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.