-
Notifications
You must be signed in to change notification settings - Fork 341
Bonfire Finders Keepers
Created by Rafase282
Github | FreeCodeCamp | CodePen | LinkedIn | Website | My Original Wiki
- Difficulty: 2/5
Create a function that looks through an array (first argument) and returns the first element in the array that passes a truth test (second argument).
Remember to use RSAP if you get stuck. Try to pair program. Write your own code.
function find(arr, func) {
var num = 0;
return num;
}
find([1, 2, 3, 4], function(num){ return num % 2 === 0; });
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.
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.
If no element satisfy the function then you must return undefined
function find(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;
}
find([1, 2, 3, 4], function(num) {
return num % 2 === 0;
});
// Using Arr.filter()
function find(arr, func) {
// filter the provide array by the function provided and return only the first element.
return arr.filter(func)[0];
}
find([1, 2, 3, 4], function(num){ return num % 2 === 0; });
- 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.
Thanks for visiting, if you like this please feel free to star my repo, follow me or even contact me about contributing as it will be a lot of work and having help would be cool.
- HTML5 and CSS
- Responsive Design with Bootstrap
- Gear up for Success
- jQuery
- Basic JavaScript
- Object Oriented and Functional Programming
- Basic Algorithm Scripting
- Basic Front End Development Projects
- Intermediate Algorithm Scripting
- JSON APIs and Ajax
- Intermediate Front End Development Projects
- Claim Your Front End Development Certificate
- Upper Intermediate Algorithm Scripting
- Automated Testing and Debugging
- Advanced Algorithm Scripting
- AngularJS (Legacy Material)
- Git
- Node.js and Express.js
- MongoDB
- API Projects
- Dynamic Web Applications
- Claim Your Back End Development Certificate
- Greefield Nonprofit Project 1
- Greefield Nonprofit Project 2
- Legacy Nonprofit Project 1
- Legacy Nonprofit Project 2
- Claim your Full Stack Development Certification
- Whiteboard Coding Interview Training
- Critical Thinking Interview Training
- Mock Interview 1
- Mock Interview 2
- Mock Interview 3