Algorithm finders keepers - thelastmile/FreeCodeCamp GitHub Wiki
The problem is quite simple to understand. You will check for each element in the array that is passed in the first argument, if the element plugged in to the function passed as the second argument returns true the first time. We do not care about the second or third one that is true, only the very first one if any. If there are none, then return undefined. This last bit is not explained but it is part of the tests used.
You can use the function directly from the parameter, no need to rename it or anything.
try to solve the problem now
You need to pass an element and record it if the function returns true, for this you just have to pass the element as the parameter for the function.
try to solve the problem now
If no element satisfy the function then you must return undefined
try to solve the problem now
Solution ahead!
function findElement(arr, func) {
// Make num undefined by default
var num;
// Loop thorugh the array and use the function to check
for (var a = 0; a < arr.length; a++) {
if (func(arr[a])) {
// Store the first case and break the loop
num = arr[a];
return num;
}
}
// otherwise return undefined
return num;
}
findElement([1, 2, 3, 4], function(num) {
return num % 2 === 0;
});
// test here
findElement([1, 2, 3, 4], function(num){ return num % 2 === 0; });
🚀 Run Code
- To make the code easier, create an undefined variable that will be returned.
- Loop through the array to check for each element if it satisfy the function. This is done by passing the arr[index of the loop] as the parameter for the function from the second argument.
- If true, then store the array element, and return it. This will stop the loop. No else needed.
- If the loop was not broken and it has ended, then return num which by default is undefined. This means that none of the elements from the array satisfied the function.
function findElement(arr, func) {
filterArr = arr.filter(func); //filter array with the function provided
return filterArr[0]; //return the first element that returns true, or undefined if no elements return true
}
// test here
findElement([1, 2, 3, 4], function(num){ return num % 2 === 0; });
🚀 Run Code
If you found this page useful, you can give thanks by copying and pasting this on the main chat:
Thanks @Rafase282 @JamesKee for your help with Algorithm: Finders Keepers
⚠️ DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.- Add an explanation of your solution.
- Categorize the solution in one of the following categories — Basic, Intermediate and Advanced. 🚥
- Please add your username only if you have added any relevant main contents. (:warning: DO NOT remove any existing usernames)
See 👉
Wiki Challenge Solution Template
for reference.