6. List 'em all! code not working


#1

Hi, can any one tell me why the code below is saying the following error:

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

Code:

var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "01234 56789",
address: ["1 street", "road", "city", "postcode"]
},
steve: {
firstName: "Steve",
lastName: "Gates",
number: "98765 43210",
address: ["2 street", "road", "city", "postcode"]
}
};

var list = function(friends) {
for (var firstName in friends) {
console.log(firstName)
}
};

Thanks


#2

@vxsunstriderxv

Look here:

var list = function(friends) {
for (var firstName in friends) {
console.log(firstName)
}
};

You must put a semicolon right after console.log(firstName) so that it ends the instructions.


#3

firstName could be changed with anything (ex. prop) because the for .. in.. doesn't check into the second object.


#4

Thanks for the suggestions. Weirdly, I tried the exact same code again today and it worked without any changes making to it!? Not sure what was going on.


#5

var list = function(friends[firstName]) {
for(var friend in friends) {
console.log(friend);
}
};

//WHY THIS ISN'T WORKING!


#6

@blogninja16183_54ebc

No need of parameter for this function because we don't need the output to change every time we call the function. So remove friends[firstName].

We iterate over the friends array by first creating variable I and as long as variable I is smaller than the length of contact, it is going to increment I variable. We are going to use function PrintPerson you've created previously with the contact array indexed at I variable.


#7

What does this even mean, its so confusing:

The "key" bit can be any placeholder name you like. It's sort of like when you put a placeholder parameter name in a function that takes arguments.


#8

What that person is saying is that the metadata is irrelevant. An example of what is being said is the following shorthand code (I'll comment the highlights):

var car = new Object();
car.doors = 4;   //on this one, the name of the "doors" property doesn't really matter.
                             // It's the  first property of the object and it's value is 4.
car.color = "blue";    //On this one, the name of the "color" property is irrelevant

console.log(car);     //the output of this will show the property names, but 
//this is not an action that would ever be called because the user of a 
//program is never needs to see the name of the property of an object 
//in the code that's being executed.

To note a difference between the shorthand dot notation that I demonstrated and the full

 var car = {
 doors: 4,
 color: "blue"
 }

each category (ie: doors, color) is called a key. I believe they are only referred to as properties in the dot notation format. Hope this helps. If not, just reply and I'll try to clarify.

<3Jon


#9

For me it gave error no matter what i did, then i changed "Bill" and "Steve" to lowercase, and then it went all good. So much wasted time on small stuff like that..

Here is my endresult:

var list = function (friends) {
for (var key in friends) {
console.log(friends[key].firstName);
}
};

var friends = {

bill: {
firstName: "bill",
lastName: "Billström",
number: "1",
address: ["Förstavägen", "Stockholm"]
},

steve: {
firstName: "steve",
lastName: "Stevens",
number: "2",
address: ["Andravägen", "Stockholm"]
}
};


#10

Here is my code for the list 'em all. It works, but logs the first names twice.
var friends = {};
friends.bill = {
firstName: "Bill",
lastName: "Gates",
number:"1-800-555-1111"
};

friends.steve = {
    firstName: "Steve",
    lastName: "Jobs",
    number:"1-800-555-2222"
    };

var list = function(obj) {
for(var firstName in friends) {
console.log(firstName);
}
};

list(friends);

However in the full code for Codecademy, their code is this:
var friends = {};
friends.bill = {
firstName: "Bill",
lastName: "Gates",
number: "(206) 555-5555",
address: ['One Microsoft Way','Redmond','WA','98052']
};
friends.steve = {
firstName: "Steve",
lastName: "Jobs",
number: "(408) 555-5555",
address: ['1 Infinite Loop','Cupertino','CA','95014']
};

var list = function(obj) {
for(var prop in obj) {
console.log(prop);
}
};
list(friends);

my question is for the placeholders.....I know it says for the key it doesn't matter what you put...I assume that is also true for where the obj is in the for/in loop. My question is why is this? Why doesn't it matter what you put in there? How does the computer know what to read to find if its not more specific to the task?
Also if you use a generic prop and obj for placeholder. If elsewhere in a program you have more code and a for/in loop can the same placeholder words be used or must they be different?
Thank you


#11

@laurardhaol.com

If you have a problem, please post in a new thread, in the appropriate sections with every details of your question (code + link).


#12

After I read this thread I wrote this correct solution

var list = function (friends) {
for (var firstName in friends) {
console.log(firstName);
}
}

Thanks guys :slight_smile: