Search for a friend - Cant find steve


#1


I can't figure out what is going on with my code, I've searched this forum for a solution but everyone's solution doesn't seem to work for me and I want to be able to learn to identify problems that occur in my own codes.


var friends = {};
    friends.bill = {
        firstName: "Bill",
        lastName: "Gates",
        number: "(123) 512-4513",
        address: []
        },
    friends.steve = {
        firstName: "Steve",
        lastName: "Jobs",
        number: "(123) 553-1412",
        address: []
        }
var list = function(friends) {
    for (var key in friends) {
     console.log(key);
    }
}
var search = function(friends) {
    for (var key in friends) {
        if(friends[key].firstName === friends){
            console.log(friends[key]);
            return friends[key];
        }
    }
}
list(friends);
search("Steve");


#2

This parameter should be a plain value variable, like name. You are passing in the entire friends object as a name defined in the argument, and quite possibly destroying the object in the process.


#3

Add prints to your code which say step by step what it's doing, compare to what it should be doing to get the desired result.

You might for example add prints which if the code was correct would print something akin to:

entering search('Steve')

looping through keys of:
{ bill:
   { firstName: 'Bill',
     lastName: 'Gates',
     number: '(123) 512-4513',
     address: [] },
  steve:
   { firstName: 'Steve',
     lastName: 'Jobs',
     number: '(123) 553-1412',
     address: [] } }

current key: bill
firstName for that key: Bill
does that value match the search? false

current key: steve
firstName for that key: Steve
does that value match the search? true

returning:
{ firstName: 'Steve',
  lastName: 'Jobs',
  number: '(123) 553-1412',
  address: [] }

#4

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