29/33: what if I have more than 2?


#1

In the lesson, they only used 2 people "Alice" and "Bill". What If I had 10+ people? How do you compare multiple stuff along with their traits? It's like the logical behavior on a file explorer in windows that sorts out everything based on the column you highlighted. (I can't say objects since that would be too literal)


#2

It depends on what you want to do. Using a loop would probably be a must either way, but how are you going to organize your people?

I would store them in an array. Now what do you want to do? You can't compare two values with 10 people. Do you want to sort them in oldest to youngest? do you want to find out who is above or below a certain age? This is really getting into simulating database query's.

// Let's bring back alice and billy to test our new function
var alice = new Person("Alice", 30);
var billy = new Person("Billy", 25);
var john = new Person("John", 50);
var bob = new Person("Bob", 35);
var michael = new Person("Michael", 23);

var people = [alice, billy, john, bob, michael];
for (x in people) {
    if(people[x].age >=30) {
        var oldpeople = [];
        oldpeople += people[x];
        console.log(people[x].name+' '+people[x].age);
    }
}

This will sort through all the people and find whoever is older than 30. It only spits out who they are, but you could store them in specific arrays for long term use, or if you wanted to get more complex, write those people into a separate file.

Also notice how you're storing them as objects in the array. This retains their other properties.