Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialjsdevtom
16,963 PointsCan I return a Boolean inside a for loop/ Why isn't my code working? - Jaascript
I want to check if all elements of a collection (first argument) have the predicate (second argument) as a key. Here are some examples:
truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex")
//true
truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex")
//false
My truthCheck function:
function truthCheck(collection, pre) {
// Go through each item in the collection
collection.forEach(function(item) {
// If item does NOT have a key matching the predicate, return false
if (!item.hasOwnProperty(pre)) {
return false;
}
});
// In theory: After cycling through the collection, if false has not been returned already, return true
return true;
}
My code always returns true. I know that if (!item.hasOwnProperty(pre)) {
is working, but the return is not exiting the function and returning false. How can I get this return false
to work?
Thanks
2 Answers
Steven Parker
231,122 PointsWhen you return from your anonymous forEach function, you're still inside the truthCheck function. The only return from truthCheck gives back true.
But you can add a variable to carry back the result of the inner test:
function truthCheck(collection, pre) {
var result = true;
// Go through each item in the collection
collection.forEach(function(item) {
// If item does NOT have a key matching the predicate, make result false
if (!item.hasOwnProperty(pre)) {
result = false;
}
});
// Finally return the result
return result;
}
Another way to fix the issue would be to replace the forEach with a normal for...in loop. You can return directly from that kind of loop as you originally intended, and it would be more efficient:
function truthCheck(collection, pre) {
// Go through each item in the collection
for (var ind in collection) {
// If item does NOT have a key matching the predicate, return false
if (!collection[ind].hasOwnProperty(pre)) {
return false;
}
}
// After cycling through the collection, if false has not been returned already, return true
return true;
}
davidschreiter
15,005 PointsHi Tom,
I think return is just breaking the anonymous function in .forEach not the truthCheck function. I would recommend to use just a traditional for loop if your going to try and break out of it early with a return statement.