7. Search for a Friend


#1



I get the error "Oops, try again. It looks like your search function doesn't return contact information for Steve."

I would appreciate if you explain the problem, not just point it out.
Please help. Thanks.


var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "(111) 111-1111",
        address: ["One Microsoft Way", "Redmond", "WA", "98052"]
    },
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: "(222) 222-2222",
        address: ["1 Infinite Loop", "Cupertino", "CA", "95014"]
    },
    jack: {
        firstName: "Jack",
        lastName: "Hughsto",
        number: "(333) 333-3333",
        address: ["123 Happy Lane", "Houston", "TX", "77004"]
    },
    james: {
        firstName: "James",
        lastName: "Hughsto",
        number: "(444) 444-4444",
        address: ["456 Happy Lane", "Houston", "TX", "77004"]
    }
};

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

var search = function(name) {
    for (var key in friends) {
        if (friends[key].firstName === name) {
            console.log(friends[key]);
            return friends[key];
        } else {
            return "The name cannot be found.";
        }
    }
};

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


#2

@jrothe22,
The problem is that you are using the return statement....
The return statement will return a Result
but it will also cause the function the be exited immediately

With a
search("Steve");
In this case the FOR-loop AND the FUNCTION are exited.....
via the ELSE as "Bill" is not "Steve"

The solution would be
-remove the return statements
-remove the ELSE statement structure...

Then use

search("Steve");
console.log( "== My End ==" );
console.log( "== All that is displayed now, is the course-checker ==" );

#3

Do you know why if I take away the "Return" and run my code now the output is one Bill listing and multiple Steve listings?


#4

@jrothe22,
As i tried to point out in my previous post.......

In some sections of this course, the course-checker Display's data
this you could then identify by placing two EXTRA code-line's:
console.log( "== My End ==" );
console.log( "== All that is displayed now, is the course-checker ==" );


#5

Para quem interessar, segue Ășltima parte do cĂłdigo, do exercĂ­cio 7:
var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "(011) 5571-0988",
address: ["Rua José Pereira", "422", "NY", "Nova York"]
},
steve: {
firstName: "Steve",
lastName: "Jobs",
number: "(021) 99775-9475",
address: ["Rua Coronel Junqueira", "899", "SP", "SĂŁo Paulo"]
},
};

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

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


#6

var friends = {}
friends.bill= {
firstName: "Bill",
lastName: "Gates",
number: "(111) 111-1111",
address: ["One Microsoft Way", "Redmond", "WA", "98052"] };
friends.steve={
firstName: "Steve",
lastName: "Jobs",
number: "(222) 222-2222",
address: ["1 Infinite Loop", "Cupertino", "CA", "95014"]};

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

var search = function(name) {
for (var key in friends) {
if (friends[key].firstName === name) {
console.log(friends[key]);
return friends[key];
} //else {
// return "The name cannot be found.";
}
};

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


#7

@jrothe22 :
I am discussing your first post at the very top okay.
You have written:

You called the function search and when the for loop starts:
It goes to the if statement which is:
if (friends[key].firstName === name)

First: it will look for key. Your first key obviously bill.

So, if **(friends["bill"].firstName === name)**
but your name input/argument is "Steve" ,so if condition failed. It should execute the else statement which is:

return "The name cannot be found.";

It's a return statement. So you cannot even see it printed/logged on the display. And it will return to the function where it was called without checking other conditions.

If you want to see the mistake(that return output "The name cannot ne found") you can console.log(search("Steve")); will give you:

bill
steve
jack
james
The name cannot be found.

Hope it will help you to find the correct way to write the code. When you can figure it out the right way to write the code .

Clue: You do not need an else statement unless you do it completely different way.

You can use return just the way you write it. If you just:
console.log(search("Steve")); it will actually log the output on the display.

Your options: to display the output
You can use:
print(search("Steve")); to print it with a printer to get a hardcopy of the function output.
You can use:
alert(search("Steve")); to get an alert of the output from the webpage.
You can do codecademy favourite and actually this is the instruction:
console.log(search("Steve"));
May be you can do a lot more thing with the output(the return value).

Always cross check with others. I might miss something.


#8

where am i doing wrong?
here is the code..
var friends=new Object();
friends.steve={
firstName:"Steve",
lastName:"smith",
number:"8861785484",
address:['rajajinagar','bangalore','560010']
};
friends.bill={
firstName:"Bill",
lastName:"gates",
number:"9845378136",
address:['shivajinagar','bangalore','560018']
};

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

            }

        }

    }
 search(steve);

#9

@methodace79068

Have a close look at

firtstName


#10

A lot of bugs in this exercise :weary: