A few minutes ago I practiced JavaScript on Codeacademy, and I found something confusing. Here is the code:
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'] }; var list = function(obj) { for(var prop in obj) { console.log(prop); } }; var search = function(name) { for(var prop in friends) { if(friends[prop].firstName === name) { console.log(friends[prop]); return friends[prop]; } else { return "contact not found"; } } }; list(friends); search("Steve");
The problem is that when I pass the string "Steve" as arg in the search function, it returns the condition "Contact not found", when I pass the string "Bill" as an argument in the same search function, it displays contact information.
How is this possible? What am I doing wrong?
source share