7. Search for a Friend -- not returning Bill



var friends= new Object ();
friends.bill= new Object ();
friends.steve= new Object ();
friends.nick= new Object ();

var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "(555) 555-5555",
address: ["here is words", "to put in a list"]

var friends = {
steve: {
firstName: "Steve",
lastName: "Jobs",
number: "(555) 555 -5555",
address: ["here is more words", "this is confusing"]

var list= function(friends) {
for (var firstName in friends)

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

Bill's information is not being returned I am so confused.


i changed my list variable to
var list= function(friends) {
for (var key in friends)


I believe you now have it right? :slight_smile:


This is the code I got to work:

var friends = {};
friends.bill = {
    firstName: "Bill",
    lastName: "Bryson",
    number: "02089945685",
    address: ["321 Fake Street", "London", "England","SE1 1AL"]
friends.steve = {
    firstName: "Steve",
    lastName: "Dangerous",
    number: "02079985685",
    address: ["The Backett Building Penthouse", "London", "England","N1 1XX"]

var list = function(targetobject){
    for (var x in targetobject){

var search = function(name){
    for (var x in friends){
        if (friends[x].firstName === name){
        return friends[x];




Ok, my issue was different. I've now changed my above post to the correct code.


You are always write (VAR friends)-its mean create new object. Delete "var", cause you are create object in first line;


I took the var out before steve and it still won't return bill :,(
thank you for pointing that out though.
Does anyone else have any suggestions? I feel like I get the material but I'm stuck and I can't move forward.


Take away the code and write again. Sometimes it helps .


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