-
Notifications
You must be signed in to change notification settings - Fork 2
Algorithm Everything Be True
🚩 Remember to use Read-Search-Ask
if you get stuck. Try to pair program 👥 and write your own code 📝
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.
Remember to iterate through the first argument to check each object.
try to solve the problem now
Only if all of them are truth will we return true, so make sure all of them check.
try to solve the problem now
try to solve the problem now
You could use loops or callbacks functions, there are multiple ways to solve this problem.
Solutions ahead!
Using for-in loop & hasOwnProperty
function truthCheck(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 is has property and value is truthy
if (collection[c].hasOwnProperty(pre) && Boolean(collection[c][pre])) {
counter++;
}
}
// Outside the loop, check to see if we got true for all of them and return true or false
return counter == collection.length;
}
// test here
truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex");
🚀 Run Code
- First I create a counter to check how many cases are actually true.
- Then check for each object if the value is truthy
- 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
Using Array.every()
function truthCheck(collection, pre) {
return collection.every(function (element) {
return element.hasOwnProperty(pre) && Boolean(element[pre]);
});
}
// test here
truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex");
🚀 Run Code
- Uses the native "every" method to test whether all elements in the array pass the test.
- This link will help Array.prototype.every()
Using Array.reduce
function truthCheck(collection, pre) {
return collection.reduce(function(acc, next) {
if (next[pre]) {
return acc;
}
else {
acc = false;
return acc;
}
},true);
}
// test here
truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex");
🚀 Run Code
- Set initial reduce value to true.
- Change it to false only when there is no key of a given name or it's falsy. Otherwise stick to the initial truthy value.
If you found this page useful, you can give thanks by copying and pasting this on the main chat:
Thanks @Rafase282 @estevanmaito @HermanFassett for your help with Algorithm: Everything Be True
⚠️ 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.
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links