Finders Keepers - func is not a function

I struggled with this, so typed in the Basic Solution code (from here) so I could see how it worked, BUT, the Console (in Chrome) keeps returning:

Uncaught TypeError: func is not a function

The following code works when I run the Test from fCC’s website, but it fails when I run it through Chrome’s Console (p.s. I’m using VS Code to write my Javascript, hence the additional code at the bottom):

function findElement(arr, func) {
	let num = 0;

	for (var i = 0; i < arr.length; i++) {
		num = arr[i];
		if **(func(num))** { **// <-- PROBLEM LINE**
			return num;
		}
	}

	return undefined;
}
let myArr1 = [1, 2, 3, 4];
let myArr2 = [1, 3, 5, 8, 9, 10];
let myArr3 = [1, 3, 5, 9];

let fE = num => num % 2 === 0;


console.log("FINDER'S KEEPERS");
console.log(myArr1);
// The following doesn't show in the Console, because of the Uncaught Error**
console.log(findElement(myArr1), fE);
console.log("");
console.log(myArr2);
console.log(findElement(myArr2), fE);
console.log("");
console.log(myArr3);
console.log(findElement(myArr3), fE);

P.P.S. “Hi”, first time posting to the forum :grinning:

You didn’t pass fE to findElement, but rather to console.log. So findElement is effectively trying to run undefined as a function. Maybe you meant this:

console.log(findElement(myArr1, fE));
1 Like

Thanks Kev! Ha! Can’t believe I didn’t see that. What a wally :grimacing:

1 Like