List 'em all!


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-3bmfN/0/6?curriculum_id=506324b3a7dffd00020bf661


Oops, try again. It looks like your list function isn't printing the right output.

var friends = new Object();
friends.steve = new Object();
friends.steve.firstName = "Steve";
friends.steve.lastName = "Jobs";
friends.steve.number = "(123)456-7890";
friends.steve.address = ["123 Apple St", "Silicon Valley", "CA"];
friends.bill = new Object();
friends.bill.firstName = "Bill";
friends.bill.lastName = "Gates";
friends.bill.number = "(098)765-4321";
friends.bill.address = ["456 Micro St", "Sillicon Valley", "CA"];
var list = function(input) {
    for (var key in friends.steve) {
       console.log(friends.steve[key]);
    }
    for (var key in friends.bill) {
        console.log(friends.bill[key]);
    }
}
/*I'm not sure where the issue is. Here is whats being printed to the console:
Steve
Jobs
(123)456-7890
[ '123 Apple St', 'Silicon Valley', 'CA' ]
Bill
Gates
(098)765-4321
[ '456 Micro St', 'Sillicon Valley', 'CA' ]
["456 Micro St","Sillicon Valley","CA"]*/


#2

Your function takes an input, but it doesn't do anything with it. Instead of using the input, you use the variables known outside the function.
Try to make a function that takes a list as parameter and loops over that list.


#3

I still don't understand.


#4

In your function you're looping over steve and bill explicity, but what if we want to add lloyd aswell?
What I'm trying to say is that you should make a list of friends and loop over that list.

Something like this:

var myFriends = {           <- don't copy this exact list!
    steve,
    bill
}

var list = function(input) {
    // loop over the input
}

list(myFriends);

#5

Sorry, but I still don't understand. According the instructions I have to print the property values of the objects. Apparently it does that but not in the way I'm supposed to?


#6

I will make the excersise myself to see if I can explain it better, but I don't have the time right now.
Send me a PM if I don't get back to you within a few hours.


#7

Let's look at this javascript section excersise by excersise:

var friends = {
    bob,
    jeff
};

3.) You are asked to add steve and bill to your friends object (bob and jeff here). Looks quite a lot like a list doesn't it?

.

var friends = {
    bob:  {
        firstName: "Bob",
        lastName: "Irwin",
        number: "0123456789"
    },
    
    jeff: {
        firstName: "Jeff",
        lastName: "Clinton",
        number: "0513336699"
    }
};

4.) You are asked to give steve and bill a firstname, a last name and a number. Looks a lot like a list within a list, right?

.

var friends = {
    bob:  {
        firstName: "Bob",
        lastName: "Irwin",
        number: "0123456789"
    },
    
    jeff: {
        firstName: "Jeff",
        lastName: "Clinton",
        number: "0513336699"
    }
};

var list = function(input) {
     // What to do?
};

6.) In this excersise you're asked to create a function that takes a single parameter. Using a for / in in they want you to print out the key.


How do we do this?
Let's look back at adding your friends to the friendsobject.

var friends = {
    bob,
    jeff
};

If we create a for / in loop we loop over each element inside friends. We will loop over bob and jeff. For example:

var friends = {
    bob,
    jeff
};

for key in friends {
    console.log(key);
}

Will result into:

bob
jeff

But what will the same for / in loop result to after the changes you made in excersise 4?

var friends = {
    bob:  {
        firstName: "Bob",
        lastName: "Irwin",
        number: "0123456789"
    },
    
    jeff: {
        firstName: "Jeff",
        lastName: "Clinton",
        number: "0513336699"
    }
};

for key in friends {
    console.log(key);
}

Result:

bob
jeff

But why? We're looping over the elements in friends, bob and jeff. Even though bob and jeff have elements themselves they will not be printed because we're only looking for the elements of friends.


What to do?
In this excersise they only want to know who your friends are, they keys inside your friends object. Only that, nothing else. There is no need to print any names, numbers or addresses.

I certainly hope this explanation has helped you as I've almost given you the answer.


#8

I understand for/in loops. I understand functions. What I don't understand is why no matter how I construct the function (even if I ONLY print the elements of the friends object), it prints the last address line (the friends.bill.address) after it prints everything else its told to and it still tells me that my list function isn't printing the right output.


#9

Nevermind. I just went back to exercise 1 and copied the code there. Thanks for you help anyway.


#10

I believe it was because you had console.log(friends.steve[key]). I had the same issue and swapping it to console.log(key); fixed it. Why that fixed it, I'm not entirely sure. Perhaps because it's now taking the actual key from the object and listing it instead of listing everything in the object itself that is under the specific object (bill or steve in this case).


#11

I had the same problem and wanted to pull my hair out.. lol Thank you @dutchc0de for your explanation! @princebatcat I think I got it?

in your for / in loop it says:

for (var key in friends) {
};

It already pulled the "key" for friends and now we just have to print it with .log inside the function.

When we insert -> friends[key] it prints the "key" of the "key" (if that makes sense) and leaves the info we needed for the exercise in the first place out.


#12

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.