curriculum/challenges/english/blocks/lab-first-element-finder/a6e40f1041b06c996f7b2406.md
In this lab, you will create a function that looks through an array and returns the first element that passes a test function (also known as a "truth test").
The function would iterate through the array and test each element using the provided test function. At the end, it would return the first element where the test function returns true.
Example:
findElement([1, 3, 5, 8], num => num % 2 === 0) // returns 8
findElement([1, 3, 5], num => num % 2 === 0) // returns undefined
Objective: Fulfill the user stories below and get all the tests to pass to complete the lab.
User Stories:
findElement that accepts an array and a function as arguments.func, given an element x, the truth test is passed if func(x) is true.undefined.You should have a findElement function.
assert.isFunction(findElement);
findElement([1, 3, 5, 8, 9, 10], function(num) { return num % 2 === 0; }) should return 8.
assert.strictEqual(
findElement([1, 3, 5, 8, 9, 10], function (num) {
return num % 2 === 0;
}),
8
);
findElement([1, 3, 5, 9], function(num) { return num % 2 === 0; }) should return undefined.
assert.strictEqual(
findElement([1, 3, 5, 9], function (num) {
return num % 2 === 0;
}),
undefined
);
findElement([1, 2, 3, 4], function(num) { return num > 2; }) should return 3.
assert.strictEqual(
findElement([1, 2, 3, 4], function (num) {
return num > 2;
}),
3
);
findElement(["hello", "world", "javascript"], function(str) { return str.length > 5; }) should return "javascript".
assert.strictEqual(
findElement(["hello", "world", "javascript"], function (str) {
return str.length > 5;
}),
"javascript"
);
findElement(["cat", "dog", "bird"], function(str) { return str.length > 10; }) should return undefined.
assert.strictEqual(
findElement(["cat", "dog", "bird"], function (str) {
return str.length > 10;
}),
undefined
);
findElement([2, 4, 6, 8], function(num) { return num % 2 === 0; }) should return 2.
assert.strictEqual(
findElement([2, 4, 6, 8], function (num) {
return num % 2 === 0;
}),
2
);
findElement([], function(num) { return num > 0; }) should return undefined.
assert.strictEqual(
findElement([], function (num) {
return num > 0;
}),
undefined
);
function findElement(arr, func) {
return arr.filter(func)[0];
}
findElement([1, 2, 3, 4], num => num % 2 === 0);