8. Victory! Search prints twice, why?


#1

So, my code works, and it passes me on to the next lesson. However, my console prints Steve's entry twice, and I'd like to know why. If I remove the return line it only prints once, but that is not correct--according to the the sample format:
console.log(friends[prop]);
return friends[prop];

What is the distinction between these two lines of code and where is my duplicate coming from?

This is my code:

var friends = {
bill: {
firstName: "Bill",
lastName: "Cranston",
number: "714-745-4282",
address: ['500', '21st Street', 'Santa Monica', 'CA', '90404']
},
steve: {
firstName: "Steve",
lastName: "Thompson",
number: "555-555-5555",
address: ['1000', 'E. 23rd Street', 'Fruitvale', 'CA', '94612']
}
};

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

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

list(friends);
search("Steve");

And this is what prints:
bill
steve
{ firstName: 'Steve',
lastName: 'Thompson',
number: '555-555-5555',
address: [ '1000', 'E. 23rd Street', 'Fruitvale', 'CA', '94612' ] }
{"firstName":"Steve","lastName":"Thompson","number":"555-555-5555","address":["1000","E. 23rd Street","Fruitvale","CA","94612"]}


#2

You can remove the console.log because return statements are used exclusively for functions.


#3

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