EDIT : Instruction from codeacademy :
Define a function search that takes a single argument, name. If the argument passed to the function matches any of the first names in friends, it should log that friend's contact information to the console and return it.
I am learning JavaScript through code academy. I am coding for a simple contact book , which have details of friends and there is asearch function to get the details of the contact/ Here is the code :
var friends = {
bill: {},
steve: {}
}
friends.bill.firstName = "bill";
friends.bill.lastName = "gates";
friends.bill.number = 587678;
friends.bill.address = ['mes quarters', 'east camp'];
friends.steve.firstName = "steve";
friends.steve.lastName = "Jobs";
friends.steve.number = 67896986;
friends.steve.address = ["nch colony", "kanjur marg"];
var list = function() {
for(var friend in friends){
// console.log(friend);
}
}
var search = function(name){
for(var prop in friends)
if(name === friends[prop].firstName){
console.log(friends[prop]);
return friends[prop];
}
}
list();
search("bill");
search("steve");
This code is not accepted as correct in the codeacademy system. It shows following error :
Oops, try again! It looks like your search function doesn't return contact information for Steve.
(Codeacademy do not show correct errors many times)
I posted this in the Codeacademy forum and asked for help. I got one reply :
To answer the question, because your data structure is messed up. Go back into the lesson to get it right:
It's like 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'] };
Can anyone explain me , what exactly wrong in my code? and what is the correct way? Here is the codeacademy forum link for my question : http://www.codecademy.com/forum_questions/52883d7c548c358401006826#answer-5288c908abf821c7450089fc