Basic scripting 7

I was debugging trying to find a solution, I added console.log(num).
It passed the test!
However, I do not fully understand why.
Can some one explain the line let num = 0; return num

I expected to use an else statement after the if statement, that returned undefined.

else {return undefined;}

I got lucky. as this is the solution.

function findElement(arr, func) {
for (let i = 0; i < arr.length; i++) {
  if (func(arr[i])) {
    return arr[i];
let num = 0;
return console.log(num);
findElement([1, 2, 3, 4], num => num % 2 === 0);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36.

Challenge: Finders Keepers

Link to the challenge:

The let num = 0 is not doing anything.

It is the second line. console.log does not have an explicit return value, so the value undefined is returned by it. You have a return before the console.log, so you are returning undefined which is correct.

1 Like

Hi @tannerpace !

I don’t think you got lucky, you just have extra stuff at the end that doesn’t need to be there :grinning:

You actually don’t need a for loop or if statement because there is a handy array method that works well for this case. If you are curious for an alternative solution.