Tell us what’s happening:
I am doing the challenge to check all objects in array return true based on second argument. I have tested with logging to console and all parts of code seem to be running correctly but the overall function keeps returning undefined. Can anyone help please?
Your code so far
function truthCheck(collection, pre) {
collection.every ((object, index) => {
if (object.hasOwnProperty(pre)) {
return true;
} else {
return false;
}
});
}
console.log(truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex"));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36
.
Challenge: Everything Be True
Link to the challenge:
Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.
Solved it myself. Needed return every block
function truthCheck(collection, pre) {
return collection.every ((object, index) => {
if (object.hasOwnProperty(pre)
&& object[pre]) {
return true;
} else {
return false;
}
});
}
console.log(truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex"));
samolex
December 13, 2020, 9:12am
3
It seems you are not using Array.every()
correctly.
Check this pseudo code
collection.every(obj => obj.has(5))
Will return true if every obj has 5 and return false if one of the obj’s does not fulfil that condition
1 Like
Thanks samolex. I got my code to work but your way looks alot cleaner so will use that going forward!
1 Like