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 trial

JavaScript

Tushar Singh
PLUS
Tushar Singh
Courses Plus Student 8,692 Points

A little problem with objects

Here's the question---

Check if the predicate (second argument) is truthy on all elements of a collection (first argument).

function truthCheck(collection, pre) {
  // Is everyone being true?
  return pre;
}

truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex");

Tasks---

1)truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex") should return true.

2)truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex") should return false.

3)truthCheck([{"user": "Tinky-Winky", "sex": "male", "age": 0}, {"user": "Dipsy", "sex": "male", "age": 3}, {"user": "Laa-Laa", "sex": "female", "age": 5}, {"user": "Po", "sex": "female", "age": 4}], "age") should return false.

4)truthCheck([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true}, {"name": "FastFoward", "onBoat": null}], "onBoat") should return false

5)truthCheck([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true, "alias": "Repete"}, {"name": "FastFoward", "onBoat": true}], "onBoat") should return true

6)truthCheck([{"single": "yes"}], "single") should return true

7)truthCheck([{"single": ""}, {"single": "double"}], "single") should return false

8)truthCheck([{"single": "double"}, {"single": undefined}], "single") should return false

9)truthCheck([{"single": "double"}, {"single": NaN}], "single") should return false

When the second argument is a string, I solved it(not a problem there)

But when the second argument is an object, I'm not sure how to solve it.(Tasks-3, 4, 7, 8, 9)

My answer

function truthCheck(collection, pre) {

  if (typeof pre === 'string') {
    for (var i=0;i<collection.length;i++) {
      if(!collection[i].hasOwnProperty(pre)) {
        return false;
      }
    }
// this part is not working
  } else if(typeof pre === 'object'){
    for(var j=0;j<collection.length;j++) {
      for(var k=0;k<collection[j].length;k++) {
        if(!collection[j][k].hasOwnProperty(pre)) {
          return false;
        }
      }
    }
  }
  return true;
}

truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex");

2 Answers

Ryan Zimmerman
Ryan Zimmerman
3,854 Points

Without digging into it to much. I would create a recursive function. You might also want to look up .some()

Tushar Singh
Tushar Singh
Courses Plus Student 8,692 Points

Really thanks. I think I can work with .some() and while I was reading about it in the documentation I thought there must be a way to check every element and luckily that happens to be .every().

Thanks a lot, you just gave me an idea :wink:

Tushar Singh
PLUS
Tushar Singh
Courses Plus Student 8,692 Points

ahaaa! As a matter of fact I can use "Boolean" as well.