-
Notifications
You must be signed in to change notification settings - Fork 340
Bonfire Everything Be True
Created by Rafase282
Github | FreeCodeCamp | CodePen | LinkedIn | Website | My Original Wiki
- Difficulty: 2/5
Check if the predicate (second argument) returns truthy (defined) for all elements of a collection (first argument).
For this, check to see if the property defined in the second argument is present on every element of the collection.
Remember, you can access object properties through either dot notation or [] notation.
Remember to use RSAP if you get stuck. Try to pair program. Write your own code.
function every(collection, pre) {
// Does everyone have one of these?
return pre;
}
every([{'user': 'Tinky-Winky', 'sex': 'male'}, {'user': 'Dipsy', 'sex': 'male'}, {'user': 'Laa-Laa', 'sex': 'female'}, {'user': 'Po', 'sex': 'female'}], 'sex');
The program needs to check if the second argument is a truthy element, and it must check this for each object in the first argument.
In JavaScript, a truthy value is a value that translates to true when evaluated in a Boolean context. All values are truthy unless they are defined as falsy (i.e., except for false, 0, "", null, undefined, and NaN).
Remember to iterate through the first argument to check each object.
Only if all of them are truth will we return true, so make sure all of them check.
You could use loops or callbacks functions, there are multiple ways to solve this problem.
function every(collection, pre) {
// Create a counter to check how many are true.
var counter = 0;
// Check for each object
for (var c in collection) {
// If it has the same property or the same property value then add 1
if (collection[c].hasOwnProperty(pre) || collection[c][pre] == pre) {
counter++;
}
}
// Outside the loop, check to see if we got true for all of them and return true or false
if (counter == collection.length) {
return true;
} else
return false;
}
every([{'user': 'Tinky-Winky', 'sex': 'male'}, {'user': 'Dipsy', 'sex': 'male'}, {'user': 'Laa-Laa',
'sex': 'female'}, {'user': 'Po', 'sex': 'female'}], 'sex');
- First I create a counter to check how many cases are actually true.
- Then check for each object if it it has the same property or the same property value. If true then add one to the counter.
- Outside the loop, I check to see if the counter variable has the same value as the length of collection, if true then return true, otherwise, return false
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