"List'em all!" doesn't work


#1



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


I don't understand what's wrong about my code with this message:
"Oops, try again. Did you create a function called list?"
When I have created the function called 'list'...


var friends = {
    
bill: {
    firstName:"Bill",
    lastName:"Gates",
    number:"(206) 555-5555",
    address: ['One Microsoft Way','Redmond','WA','98052']
    },

steve: {
    firstName:"Steve",
    lastName:"Jobs",
    number:"(602) 333-4444",
    address: ['1 Infinite Loop','Cupertino','CA','95014']
    },
};

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


#2

The instruction states:

In the loop, use console.log to print out the key. (For example, if you only have bill and steve as entries, list should just print out "bill" and "steve".)

So you should just log the key (bill in your case), instead of the firstName (which is harder to do with a for in loop).


#3

OMG, it works! But how?
Why this for/in loop print out 2 keys in our object "friends", when put only var "bill" as parameter in the loop ?


#4

Because bill is just the name that you chose to give it. A better name might have been friend, because it gets the value of each friend in friends.


#5

I think I got it... The key in for/in loop successively gets the value of each object in our object "friends"... Right...?


#6

You got it! @igorek9191


#7

Can you help me with "search" function:

var search = function (name) {
        for (var friend in friends) {
            if (friends[friend].firstName===name) {
                console.log(friends[friend]);
                return friends[friend];
            } else {
                console.log("No matches");
            }
        }
    };
    search('Bill');

because it gives wrong result:

{ firstName: 'Bill',
  lastName: 'Gates',
  number: '(206) 555-5555',
  address: [ 'One Microsoft Way', 'Redmond', 'WA', '98052' ] }
No matches
{ firstName: 'Steve',
  lastName: 'Jobs',
  number: '(602) 333-4444',
  address: [ '1 Infinite Loop', 'Cupertino', 'CA', '95014' ] }
No matches
{ firstName: 'Steve',
  lastName: 'Jobs',
  number: '(602) 333-4444',
  address: [ '1 Infinite Loop', 'Cupertino', 'CA', '95014' ] }
{ firstName: 'Bill',
  lastName: 'Gates',
  number: '(206) 555-5555',
  address: [ 'One Microsoft Way', 'Redmond', 'WA', '98052' ] }
{"firstName":"Bill","lastName":"Gates","number":"(206) 555-5555","address":["One Microsoft Way","Redmond","WA","98052"]}

#8

What are you feeding it as input? It looks correct? Or are you expecting it to do something different?


#9

As input I give the name 'Bill'
I expect it logs friend's contact information to the console, found by if/else statement. Unfortunatly now it logs all information about friends in mixed way.


#10

If I try it, it passes :smiley: !


#11

var friends=new Object();
friends.bill= new Object();
friends.steve=new Object();
friends.bill.firstName="Bill";
friends.bill.lastName="Gates";
friends.bill.number="8686058016";
friends.bill.address=["Microsoft","USA"];
friends.steve.firstName="Steve";
friends.steve.lastName="Jobs";
friends.steve.number="8125177030";
friends.steve.address=["Apple","USA"];
var list = function (friends) {
for (var bill in friends) {
console.log(bill);
}
}


#12

Hallo Guys! I have some problem to, It anwesrs me that my list function isn't printing the right output.

Here is my code

var friends = {

bill : {
    firstName : "Bill",
    lastName : "Gates",
    number: "9439340340808",
    address : ['9 Park Ave.', 'Yerevan', 'Armenia'],
    },

steve :  {
    firstName : "Steve",
    lastName : "Jobs",
    number : "5497349043",
    address : ['99 Arshakunyac Str.', 'Yerevan', 'Armenia'],

    },

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

};

Its printing contacts two times. Plese help me!