7. Search for a friend. Problem


#1
    var friends = {
    bill: {
        firstName:"Bill",
        lastName: "Gates",
        number: "555 555 1234",
        address: ['One Microsoft Way','Redmond','WA','98052']
    },
    steve: {
        firstName: "steve",
        lastName: "Jobs",
        number: "555 555 1235",
        address: ['1 Infinite Loop','Cupertino','CA','95014']
    },
    dave:{
        firstName: "Dave",
        lastname: "Watts",
        number: "0208 555 1234",
        address: ["Number 1 Dave's Street", "London","UK","NW1 0NA"] 
    }
};

var list = function (obj) 
{
for (var key in obj) 
{
console.log(key);
}
};

what's bad here???
I get "Oops, try again. It looks like your search function doesn't return contact information for Steve."
tried to copy code from this forum...


#2

Is this the full code?
As far as I can tell, the problem is that the first letter in "Steve" isn't capitalized.


#3
var search = function(name) 
{
for(var prop in friends) 
{ if(friends[prop].firstname === name) {
console.log(friends[prop]);
return friends[prop];
}
}
};
list(friends);
search("steve");

oh here is more code... When capitalized getting same...


#4

I'm getting the same exact error for this code:

var friends ={
bill:{
fistName: 'Bill',
lastName: 'Smith',
number: '555-5501',
address: ['1 Cargill Ave', 'Toronto', 'Ontario', 'Canada']
},
steve:{
firstName: 'Steve',
lastName: 'Jones',
number: '555-5502',
address: ['10 Cargill Ave', 'Toronto', 'Ontario', 'Canada']
}
};
var list = function (friends) {
for (var prop in friends) {
console.log(prop);
}
};

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

}
};
list(friends);
search('Bill');

I have no idea what's wrong.


#5

I also tried this variation of code and still get the same error:

var search = function (friends) {
for (var key in friends) {
if (friends[key].firstName===name) {
console.log(friends[firstName, lastName, number, address]);
return(friends[key]);
}
else {
console.log("Sorry, I don't see that name");
}
}
};

list(friends);
search('Steve', 'Bill');


#6

Capitalize S in Steve in both the object "firstName:"Steve" and also in the last line of code "search("Steve"). It should work then. I had this problem too.


#7

I getting the same error...


#8

what is PROP?? i dont see that anywhere in the lesson, and I cant figure it out. Please someone tell me where PROP comes from


#9

I think "prop" was borrowed from here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in
...short for "property".

I am not 100% sure, but I think I understand prop as a key or random variable you can choose, like 'x', 'i', etc. The for-in loop is iterating through each property (defined as prop/x/i/etc) of the object, thus the function keeps running through the loop until it has completed returning all the values in the friends list. As I understand it, you can use any non-numeric value there in place of "prop" as long as it stays consistent throughout the function.

Example:

function list(friends){
    for(var blahblah in friends){
        console.log(x);
    }
}

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

#10

changing the variable "key" into different thing was helpful, thank you :slightly_smiling:


#11

if(friends[prop].firstname === name)
from the above line change firstname to firstName