List them all (ex 6). Not able to produce the output


#1


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


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

console output:
undefined
undefined
undefined
undefined

var friends = {
bill: {
firstName: "Bill",
lastName: "Bryan",
number: "(307) 354-*1247",
address: ['212 Taft Ave', 'Pocatello', 'ID', '83201']
}
};

var friends = {
steve: {
firstName: "Steve",
lastName: "Voorhies",
number: "(801) 435-6877",
address: ['142 Alta Dr', 'SVR', 'WY', '83704']
}
};

var list = function(friends){

for(var firstName in friends.bill) {
    console.log(bill.firstName); 

}

for(var firstName in friends.steve) {
            console.log(this.firstName );//(this) is an instance of steve
}

};


#2

your friends object should contain both bill and steve:

friends = {
  bill: {

  },
  steve: {

  }
}

then you can loop over friends to list both bill and steve. Please read the instructions again, and try again, if you need more help after trying this, post an update version of your code


#3

Hi stetim94 your suggestions helped a lot (combining both bill and steve under 1 friends object and also adding a comma to separate each instance). I have read and re-read the instructions many times and have had 3 different times at coming back to this today and still no luck. I also like you didn't give me the answer. I truly do want to solve this exercise myself. Is there a further hint you can give that will nudge me in the right direction? Here is my updated code but it's not much further along than when I first created the topic (as I am still very stuck and not yet seeing the bigger picture of where I am missing the point).

var friends{
    bill:  {
        firstName: "Bill",
        lastName: "Bryan",
        number: "(307) 354-*1247",
        address: ['212 Taft Ave', 'Pocatello', 'ID', '83201']
    },
    
    steve: {
            firstName: "Steve",
            lastName: "Voorhies",
            number: "(801) 435-6877",
            address: ['142 Alta Dr', 'SVR', 'WY', '83704']
    }
};

var list =  function(name){


    for(var name in friends) {
        console.log(bill.firstName); 
         console.log(steve.firstName );
    }

};

#4

there is a equal sign missing on the first line:

var friends{

the function parameter contains the friends object, you can loop over it and then log it to console.


#5

after setting all the properties of firstName, lastName, number & address, simply create your list variable, passing in one argument (friendlist) in this case and call the for method on the name variable before writing it to console as follows:

var list = function(friendlist) {
for(var name in friendlist) {
console.log(name);
}
};


this is what i had above:

var friends = new Object();

friends.bill = new Object();
friends.bill.firstName = "Bill";
friends.bill.lastName = "Gates";
friends.bill.number = 1;
friends.bill.address = ['10260 Bandley Dr.','Cupertino','CA','95014'];

friends.steve = new Object();
friends.steve.firstName = "Steve";
friends.steve.lastName = "Jobs";
friends.steve.number = 1;
friends.steve.address = ['1000 Infinity Loop.','Cupertino','CA','95016'];


#6

I want to first thank both Io_code and stetim94 for your combined help. I succeeded at completing the exercise and would have struggled for some time had you both not taken the time to offer your thoughts.

One more thing though as it will help to clarify something I am not understanding. Please note I changed my approach and altered what I first tried to attempt the lesson with an object literal notation and instead went with what became the easier object constructor approach.

Even though I passed the lesson, I shouldn't have because I only instantiated a friends object but never once did anything with it (such as friends.bill) ...

So I passed the lesson but I'm not understanding the output logged to the console:

Bill
Bill
Bill
Bill
Steve
Steve
Steve
Steve
["142 Alta","SVR","WY","83704"]
...

Here is the code (I've added some things I need some clarification on that I put below my code):

var friends = new Object();

var bill = new Object();
        bill.firstName = "Bill";
        bill.lastName ="Bryan";
        bill.number=  "(307) 354-*1247";
        bill.address= ['212 Taft', 'Irwin', 'ID', '83201'];
    
var steve = new Object();
        steve.firstName= "Steve";
        steve.lastName= "Voorhies";
        steve.number= "(801) 435-6877";
        steve.address= ['142 Alta', 'SVR', 'WY', '83704'];
    

var list =  function(nameList){
    for(var nameList in bill) {
        console.log(bill.firstName);
    }
    
    for(var nameList in steve) {
        console.log(steve.firstName);
    }    
};
  • Why does it print/log 4 firstName keys for each bill and steve object?
  • Why is the address key of steve logged?

The only interpretation I could come up with is this:
- Each object contains four keys
- The for in loop for each object will iterate the firstName key and log it to the console and stop iterating once the number of keys in each object is displayed 4 times.
- (i have to really stretch my imagination on this one). After the final for in loop of the last object is logged then the last key of the 2nd steve object gets logged as it's the last item that was iterated.


#7

the codecademy console might make some function calls which is displayed in the output.

I am so confused by your code, why can't simply nest bill and steve inside friends:

var friends{
    bill:  {
        firstName: "Bill",
        lastName: "Bryan",
        number: "(307) 354-*1247",
        address: ['212 Taft Ave', 'Pocatello', 'ID', '83201']
    },
    
    steve: {
            firstName: "Steve",
            lastName: "Voorhies",
            number: "(801) 435-6877",
            address: ['142 Alta Dr', 'SVR', 'WY', '83704']
    }
};

and then loop over them:

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

when you call the function (last line) you pass friends as argument, which is then stored in the function parameter (nameList), which you can loop over and then log

I don't understand the complicated it way you do it, well i understand, but it is wrong and confusing.


#8

Hi Stetim94,

When you compare what i first submitted and what you've suggested we were pretty much on the same page (i only need to slightly tweak the initial approach to complete the exercise). Also, I do appreciate and understand where you're coming from when comparing my first & last submissions. Io-code had their school of thought added in and i changed course.

What i had was pretty much by the book of the lesson progression. Io-code had suggested a different approach and I thank you both.

.. Here is my updated code but I get an error:
(console) SyntaxError: missing before statement
(editor) Oops, try again. There was a problem with your syntax.

//var friends =new Object(){

var friends {
    bill: {
        firstName: "Bill",
        lastName: "Bryan",
        number:  "(307) 354-1247",
        address: ['212 Taft', 'Irwin', 'ID', '83201']
    },
    steve: {
        firstName: "Steve",
        lastName: "Voorhies",
        number: "(801) 435-6877",
        address: ['142 Alta', 'SVR', 'WY', '83704']
    }
};
    

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

list (friends);

#9

equal sign is missing on this line:

var friends = {

You should read about the different ways to create objects.

Yea, but you had two loops in your last code, and looped over things which didn't make much sense


#10

Yep that solved it. Thanks for your time and help


#11