Question 7 Search, Reference Error: Can't find variable: steve


#1

Hello everyone,

I can't seem to understand what's wrong with my code. I get the error "Reference Error: Can't find variable: steve"

var friends={
tino: {
firstName: "Tino",
lastName: "Heredia",
number:"\(555\) 555-5555",
address: ['Street','Unit','Key Biscayne','FL','33149']
},
david: {
firstName: "David",
lastName: "Riascos",
number:"David\'s Number",
address: ['Street','Unit','Key Biscayne','FL','33149']
},
bill: {
firstName: "Bill",
lastName: "Jah",
number:"Bill\'s Number",
address: ['This','is','Bill\'s','AD','dress']
},
steve: {
firstName: "Steve",
lastName: "Rastafari",
number:"Steve\'s Number",
address: ['This','is','Steve\'s','AD','dress']
}
};
var list = function(friends){
for(var key in friends){
console.log(key)
}
};
var search = function(friends){
for(var firstName in friends){
if(friends[key].firstName === steve){
console.log(friends[key]);
return friends[key];
}
}
};
list(friends)
search(steve)

Thank you so much!


#2

steve is a string, you must use "steve".
....
if(friends.firstName === "steve"){
console.log(friends[key]);
return friends[key];
}
}
};
list(friends);
search("steve");


#3

That did help! However now I'm having another problem. It's not returning the contact info for steve. Here's my code with the string in place:

var friends={
tino: {
firstName: "Tino",
lastName: "Heredia",
number:"\(561\) 373-6380",
address: ['300 Galen Dr','204','Key Biscayne','FL','33149']
},
david: {
firstName: "David",
lastName: "Riascos",
number:"David\'s Number",
address: ['300 Galen Dr','204','Key Biscayne','FL','33149']
},
bill: {
firstName: "Bill",
lastName: "Jah",
number:"Bill\'s Number",
address: ['This','is','Bill\'s','AD','dress']
},
steve: {
firstName: "Steve",
lastName: "Rastafari",
number:"Steve\'s Number",
address: ['This','is','Steve\'s','AD','dress']
}
};
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 === "steve"){
console.log(friends[key]);
return friends[key];
}
}
};
list(friends)
search("steve")


#4

Hi, i checked your function for returning the Steve's contact. There are some issues.
Your function uses the same keywords of the object. In my opinion this isn't a good practice.
I suggest that you modify the keyword, so you can follow your actions of function.

var search = function(friends){ the parameter is the same of the object, not good;
for(var key in friends){ you search in the object, but you already have a parameter "steve";
if(friends[key].firstName === "steve"){ when you find "steve" in friends object, you compare the firstName that will be "Steve" not "steve", this condition never happens;
console.log(friends[key]); you print on screen the object, but if the compare is false never happens
return friends[key]; again you want print something.

this is my code, try it

var search = function(key){
if(key === "steve"){
console.log(friends[key]);
//return friends[key];
}
};

This code still no good practice, because still use keyword not good, but can help you to follow the difference from your code.

Best Regards


#5

I tried changing the keywords as well as the capitalization in the search functions. The function works now and the lesson is telling me that I completed the problem. However, if I try to execute the search function in the following manner:
search("Bill")
It still gives me the search results for "Steve".

Here is my new code:

var friends={
tino: {
firstName: "Tino",
lastName: "Heredia",
number:"\(561\) 373-6380",
address: ['300 Galen Dr','204','Key Biscayne','FL','33149']
},
david: {
firstName: "David",
lastName: "Riascos",
number:"David\'s Number",
address: ['300 Galen Dr','204','Key Biscayne','FL','33149']
},
bill: {
firstName: "Bill",
lastName: "Jah",
number:"Bill\'s Number",
address: ['This','is','Bill\'s','AD','dress']
},
steve: {
firstName: "Steve",
lastName: "Rastafari",
number:"Steve\'s Number",
address: ['This','is','Steve\'s','AD','dress']
}
};
var list = function(key){
for(var key in friends){
console.log(key)
}
};
var search = function(key){
for(var key in friends){
if(friends[key].firstName === "Steve"){
console.log(friends[key]);
return friends[key];
}
}
};
list(friends)
search("Bill")

This is the result I get:
tino
david
bill
steve
{ firstName: 'Steve',
lastName: 'Rastafari',
number: 'Steve\'s Number',
address: [ 'This', 'is', 'Steve\'s', 'AD', 'dress' ] }
{"firstName":"Steve","lastName":"Rastafari","number":"Steve's Number","address":["This","is","Steve's","AD","dress"]}


#6

hi, please redo the search function with another variable in the for cicle. You still use the same variable name for everything. It isn't a good practice.