7/8 "It looks lika search function doesn't return contact information for Steve


#1

Helo, please help me, here is my code:

var friends = {};
friends.Bill = {
firstName: "Bill",
lastName: "Gates",
phoneNumber: "48113327",
address:["Kongsteinsvegen", "Strand", "Tau", "4120"],
}
friends.Steve = {
firstName: "Steve",
lastName: "Jobs",
number: "49493929",
address: ["One Microsoft Way", "Redmond", "WA", "98052"],

};

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

var search = function(name){
for(var key in friends) {
if(friends[key].firsName === name) {
console.log(friends[key]);
return friends[key];
}
else {
return "No match found"
}
}
};

list(friends);
search("Bill");

Why doesn't it work?


#2

Instead of

if(friends[key].firstName === name)

you put

if(friends[key].firsName === name)

It's just a little typo that would be hard to spot.

Hope it helped! :smile:


#3

Thank you! I fixed it, but I get the same "Oops, try again! message".
But now this is what`s printed in the console:

Bill Steve { firstName: 'Bill', lastName: 'Gates', phoneNumber: '48113327', address: [ 'Kongsteinsvegen', 'Strand', 'Tau', '4120' ] } "No match found"

So, to me it looks like Im printing.
Both first names, then all of the content in Object "Bill".
And then it prints the console.log string in the else part of the if/else statement.

Any Ideas? :smile:


#4

Hmmmm...

This is my code:

var friends = {};
    friends.bill = {
        firstName: "Bill",
        lastName: "Parl",
        number: "(666) 452-6138",
        address: ['Valley Way' , 'Montey', 'CA' , '94568']
};
    friends.steve = {
        firstName: "Steve",
        lastName: "Unot",
        number: "(334) 614-7823",
        address: ['Dell City' , 'Vancouver', 'CA' , '97358']
};

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

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

list(friends);
search("Steve");

There may be another problem in your code, for I can not see any other problem. This code should work, for it works with mine.

Try it! :wink: If it doesn't work... Sorry I'm not the best debugger.


#5

Thank you!
I compared your code with mine, and found a couple of more errors.
:blush:


#6

In your search function you are using a return-statement in your ELSE code-block.
change this into
console.log("No match found");

The return-statement causes an EXIT from your current-function-scope,
in your Original search function 'Steve' would never be found.

= = = = = = = = = = = = = = = = = = =

Reference::

google search
=the book=
javascript variable site:developer.mozilla.org
= https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var
= https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals
javascript return statement site:developer.mozilla.org
= https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return
=discussions=
javascript return statement site:stackoverflow.com
= http://stackoverflow.com/questions/3717420/is-there-a-benefit-to-using-a-return-statement-that-returns-nothing
= http://stackoverflow.com/questions/22134753/return-statement-in-javascript

=============================================

nice to know

You could test in labs.codecademy.com
choose Javascript
You will get a split-screen
In the left-hand-side you paste-in your code
click on the Run button.
The right-hand-side is the console

references

= www.crockford.com --> for most respected guide-line in Javascript
like coding convention
= www.crockford.com
= http://javascript.crockford.com/code.html
= http://javascript.crockford.com/survey.html


#7

I found out what the problem was.. turns out the codecademy editor is EXTREME nit picky. I was scratching my head for a good 30 minutes and it kept saying "Looks like search function doesn't return contact information for Steve"

Turns out that you need to put a CAPITAL (S) in Steve. I had mine with all lower case and it kept bugging me so much. Capitalized the first letter in the name and voila! problem solved.


#8

I don't understand why this code isn't working.
It gives me the error "Oops, try again. It looks like your search function doesn't return contact information for Steve."

Please help me out.

var friends=new Object();
friends.Bill= new Object();
friends.Steve=new Object();

friends.Bill={firstName:"Bill",
lastName:"gates",number:1234,
address:[917,'Ncharles']
}

friends.Steve={firstName:"Steve",
lastName:"jobs",number:6789,
address:[913,'Ncharles']
}
var list = function(contacts)
{
for(var key in contacts)
{
console.log(key);
}
};

list(friends);

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

search("Steve");


#9

Oops, Please ignore. I got the ERROR. I was using firstname instead of firstName.


#10

...Don't the directions say to find the name using Steve's last name? (I think)


#11

And you are using:

that will not function either...