Search Problems


#1

I’m having trouble writing a working function to search for a name in Organization with Objects, Lesson 7.
Although CodeAcademy passed me on it, I would like to know why it isn’t behaving the way I want. I want to prompt the user to search for a name (to be converted to uppercase), then either console.log the results or have a pop up with a ‘No results found’ message. However, when I ran the code the pop up had to be clicked several times instead of once and there were no results found. This exercise was a bit difficult for me…

```

var friends = {};
friends.bill = {
firstName: ‘BILL’,
lastName: ‘STEVENS’,
number: 555-555-5555,
address: [‘23 Privet Dr’, ‘Little Whinig’, ‘UK’, ‘23495’]
};
friends.steve = {
firstName: ‘STEVE’,
lastName: ‘WILLIAMS’,
number: 333-333-3333,
address: [‘134 Long Ave’, ‘Drury’, ‘OK’, ‘77394’]
};
friends.jo = {
firstName: ‘JO’,
lastName: ‘TSCHOZKI’,
number: 745-284-2294,
address: [‘2034 Avenue of the Americas’, ‘Apt 23C’, ‘New York’, ‘NY’, ‘19290’]
};

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

var search = function(){
var name = prompt(“Search a first name here”).toUpperCase();
for (var key in friends){
if(friends[key] === name){
console.log(friends[name]);
return(friends[name]);
}
else{
confirm(‘No results found’);
}
}
}

<do not remove the three backticks above>

#2

Hi @mlacy248 :smiley:
Yes, this exercise is quite challeging to some people!
But however, in your if statement within your for loop, you need to remember to access the friend’s firstName property too by using the dot notation!
-> friends[key].firstName <-
Then in your console.log statement, you need to access the actual name of the friend, not the variable you input. So basically the keys in friend… hint hint

Hope this helps!!!


#3

I tried to fix it using your advice but it still didn’t work so I tried to make the code simpler but it still isn’t working. Here’s the new code:
var friends = {};
friends.bill = {
firstName: ‘BILL’,
lastName: ‘STEVENS’,
number: 555-555-5555,
address: [‘23 Privet Dr’, ‘Little Whinig’, ‘UK’, ‘23495’]
};
friends.steve = {
firstName: ‘STEVE’,
lastName: ‘WILLIAMS’,
number: 333-333-3333,
address: [‘134 Long Ave’, ‘Drury’, ‘OK’, ‘77394’]
};
friends.jo = {
firstName: ‘JO’,
lastName: ‘TSCHOZKI’,
number: 745-284-2294,
address: [‘2034 Avenue of the Americas’, ‘Apt 23C’, ‘New York’, ‘NY’, ‘19290’]
};

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

function search(name) {
for (var key in friends){
if(friends[key].firstName === name){
console.log(friends[key].firstName);
return(friends[key].firstName);
}
else{
console.log(‘No results found’);
}
}
}
search(STEVE);


#4

Okay almost there, remove the console.log statement -> return(friends[key]) to return the actual name -> console.log(search(steve));

I hope this finally resolves your code problems!!!


#5

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