7.search for a friend - messy output


#1


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


this is the output it shows me:

{ firstName: 'Steve',
lastName: 'Dough',
number: '0542349258',
address: [ 'bla bla', 'road', '4' ] }
{ firstName: 'Steve',
lastName: 'Dough',
number: '0542349258',
address: [ 'bla bla', 'road', '4' ] }
{ firstName: 'Steve',
lastName: 'Dough',
number: '0542349258',
address: [ 'bla bla', 'road', '4' ] }
{ firstName: 'Bill',
lastName: 'Smith',
number: '054123456',
address: [ 'bla', 'nes tsiona', '5' ] }
{"firstName":"Steve","lastName":"Dough","number":"0542349258","address":["bla bla","road","4"]}

why does it show me bill and why is everything written a bunch of times?


var friends = {
   bill: {
     firstName: "Bill",
     lastName: "Smith",
     number: "054123456",
     address: ["bla", "nes tsiona", "5"]
     },
   steve: {
    firstName: "Steve",
    lastName: "Dough",
    number: "0542349258",
    address: ["bla bla", "road", "4"]
    }
};

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

var search = function (fname) {
    for (var key in friends) {
        if (friends[key].firstName === fname) {
            console.log(friends[key]);
            return friends[key];
        } 
    }
};

search("Steve");


#2

because the exercise needs to validate your code, and this output is also shown. If you want to see what your code really yields, run it on jsbin (select js and console tab)


#3

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