Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

jsdevtom
jsdevtom
16,963 Points

Can 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
Steven Parker
220,425 Points

When you return from your anonymous forEach function, you're still inside the truthCheck function. The only return from truthCheck gives back true.

:point_right: 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;
}

Hi 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.