/*
TODO:
Create a function that looks through an array arr and returns the first element in it that passes a 'truth test'.
This means that given an element x, the 'truth test' is passed if func(x) is true.
If no element passes the test, return undefined.
1. create num function and leave it at 0
2. create for loop to loop through array
a.
b.
3.inside for loop create if statement to compare func to the array
a. Integrate Num into if statement
4. place that value in num and return num
*/
function findElement(arr, func) {
for (let i = 0; i < arr.length; i++) {
let num = arr[i];
if (func(num[i]) == true) {
return true
} else {
return undefined
}
}
}
console.log(findElement([1, 2, 3, 4], num => num % 2 === 0));
Hey everyone! I’m having trouble with how num is supposed to fit in because it provides the first test case as undefined which is fine since that what it’s supposed to return but I can’t get the first to come as a true
I’m open to suggestions
P.S I will be responding tomorrow morning as I’m offline after I post this.
What? What? arr is an array of numbers, therefore num is a number. So, what is num[i]? I would expect that to always be undefined. To be honest, I don’t understand why you need the num variable.
Also, when you find the match, what are you supposed to return?
And the return undefined - it’s the right idea but in the wrong place. Because you have an if/else and each of them return, you will never get past the first element. You need to move return undefined somewhere where it will only return after everything has been checked.
If I fix those three things, your code passes for me.
OK I have debugged and tested and it works what’s you feedback on this answer:
function findElement(arr, func) {
let num;
for (let i = 0; i < arr.length; i++) {
if (func(arr[i]) == true) {
let num = arr[i];
return num;
}
}
}
I finally found that num doesn’t actually mean anything so instead I did what you said and got rid of else and instead num undefined before the if statement because that way num would only return something if it was true and return undefined if it did not pass.
Yeah, that’s basically what I have. I actually moved the return undefined outside of the loop. You removed it which works too because not returning anything is the same as returning undefined.
Thanks! I’m still a bit confused over why the test case uses num if num isn’t an input in the function, but I suppose that question is best left alone. I also am not sure how returning arr[i] brings 8. Does it bring the value that the loop was at when the if statement came true? That’s my theory but thanks for the feedback and help!
It’s just the arbitrary parameter name used in the callback function. It has nothing to do with the functioning of findElement - which doesn’t know what variables are called inside the callback function and doesn’t care.
So,
function findElement(arr, func) {
// ...
}
findElement([1, 2, 3, 4], num => num % 2 === 0);
// findElement([1, 2, 3, 4], function(num) { return num % 2 === 0; });
could be
function findElement(arr, func) {
// ...
}
const myData = [1, 2, 3, 4];
const myCallback = num => num % 2 === 0;
findElement(myData, myCallback);
The findElement function doesn’t care one iota what is going on inside that callback. You could also do: