Search for a friend. It's really confusing


#1



It keeps saying I didn't create a function called search. Though there's probably a few things I have wrong there, I don't know.


var friends = {
    bill:{
        firstName: "Bill",
        lastName: "Gates",
        number: "(954) 666-6666",
        address: ['One Microsoft Way', 'Microsoft', 'PC', '100']
    },
    steve:{
        firstName: "Steve",
        lastName: "Jobs",
        number: "(954) 999-9999",
        address: ['IPHONES Way', 'Apple', 'FL', '33024']
    }
};

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

var search = function(name) {
    for(var x in friends){
        if(friends[someName].firstName === name){
            console.log(friends[someName]);
            return friends[someName];
        }
        else{ 
            console.log("error")
        }
    }
};


#2

Does someName == x ?


#3

Lol, thank you. Don't know how I got that mixed up like that. So i fixed that problem. But when it read out it reads out like this, it logs one of their info twice and the other only once. Also why does it keep saying error.

error
{ firstName: 'Steve',
  lastName: 'Jobs',
  number: '(954) 999-9999',
  address: [ 'IPHONES Way', 'Apple', 'FL', '33024' ] }
error
{ firstName: 'Steve',
  lastName: 'Jobs',
  number: '(954) 999-9999',
  address: [ 'IPHONES Way', 'Apple', 'FL', '33024' ] }
{ firstName: 'Bill',
  lastName: 'Gates',
  number: '(954) 666-6666',
  address: [ 'One Microsoft Way', 'Microsoft', 'PC', '100' ] }

#4

Study your code in the context of an unordered object. There is no specific order for the the loop to iterate the object, so if the first key does not point to a matching name, the else clause goes into effect. By rights, there should not be an else clause for this if statement.


#5

Please tell me what's wrong with this

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

error : Oops, try again. It looks like your search function doesn't return contact information for Steve.


#6

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