Issues with Search function


#1


Contact List


I dont see the issue with my code, the compiler shows

steve
Something wrong
Something wrong
Something wrong
Something wrong

then gives a warning.

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


Replace this line with your code. 
friends = {
 bill: {
    firstName: "Bill",
    lastName: "Gates",
    number: "(202) 555-5555",
    address:['One Microsoft','Redmond','WA','98052']
  }
};
    
friends = {
 steve: {
    firstName: "Steve",
    lastName: "Jobs",
    number:"(202) 555-5555",
    address:['Apple','Redmond','WA','98052']
  }
};

var list = function(param){
    for(var key in friends){
        console.log(key);
    }
};
    
var search = function(name){
    for(var key in friends){
        if(friends[key].firtstName === name){
            console.log(friends[key]);
            return friends[key];
            }else{
                console.log('Something wrong');
                }
        }
};
    
    
list(friends);
search('steve');


#2

friends = {
 bill: {
    firstName: "Bill",
    lastName: "Gates",
    number: "(202) 555-5555",
    address:['One Microsoft','Redmond','WA','98052']
  }
};
    
friends = {
 steve: {
    firstName: "Steve",
    lastName: "Jobs",
    number:"(202) 555-5555",
    address:['Apple','Redmond','WA','98052']
  }
};

Your dictionary friends have just one dictionary in it, steve not bill.
so your code is throwing an error!

Your dictionary should be something like this..

friends = {
 bill: {
    firstName: "Bill",
    lastName: "Gates",
    number: "(202) 555-5555",
    address:['One Microsoft','Redmond','WA','98052']
  },
 steve: {
    firstName: "Steve",
    lastName: "Jobs",
    number:"(202) 555-5555",
    address:['Apple','Redmond','WA','98052']
  }
};

Now friends consist of both items (i.e steve and bill ) details.

do tell if it solved your problem!
I hope it helps! :slight_smile:


#3

Still having some issues with getting the correct answer.


#4

What error it's throwing now?


#5

Its saying the function doesn't return information for steve


#6

Well I just tested the code, It works fine for me

did you noticed this?

Comma separating both items?


#7

var friends = {};

friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "(202) 555-5555",
address:['One Microsoft','Redmond','WA','98052']
},
steve: {
firstName: "Steve",
lastName: "Jobs",
number:"(202) 555-5555",
address:['Apple','Redmond','WA','98052']
}
};

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

var search = function(name){
for(var key in friends){
if(friends[key].firtstName === name){
console.log(friends[key]);
return friends[key];
}else{
console.log('Something wrong');
}
}
};

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

Still same error


#8

compiler does:

bill
steve
Something wrong
Something wrong
Something wrong
Something wrong
Something wrong
Something wrong
Something wrong
Something wrong


#9

S in steve is lowercased!
u have used uppercased S
search('Steve');

do tell if it solved it!
i hope it works!


#10

Yeah idk what it is I got another assignment I need to turn in at 11:59, could you get it to work on oyur compiler?


#11

I tried making all the letters the same case still didn't work.


#12

I appreciate your help thus far greentreemee


#13

It does work on my compiler. Change the browser ,maybe its causing the problem.


This is your code that works fine and let me pass to next problem.
I just changed 'Steve' in to 'steve' .


#14

tried a different browser and everything still nothing


#15

Maybe try to delete cache or history , I tested and even showed that Its working fine.
I hope it helps :slight_smile:


#16

I had the same problem. I looked through a lot of topics and found the answer in this: https: //discuss.codecademy.com/t/is-this-supposed-to-happen/79960
You need to remove else statement from the loop and search will work correctly.
But how to put console.log('Something wrong') outside the loop and make it work i don't know how yet!
If you have already solved the problem, then tell me how, please!


#17

You just have a typo in this line

if(friends[key].fir**t**stName === name){


#18

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