Exercise 1.7 "search function doesn't return contact information for Steve."


#1


https://www.codecademy.com/courses/javascript-beginner-en-3bmfN/0/7?curriculum_id=506324b3a7dffd00020bf661#


I have been trying to get through this exercise using the code below. I keep getting an error message that says "it looks like your search function doesn't return contact information for Steve."

What I get on the output screen is a listing of all of the elements of bill, followed by "Nobody by that name" which means my for-in loop is outputting Bill's information, but then doesn't move on to Steve for some reason. The right-hand panel displays the following output:

[Function]
{ firstName: 'Bill',
lastName: 'Gates',
number: '(206) 555-5555',
address: [ 'One Microsoft Way', 'Redmond', 'WA' ] }

What am I doing wrong?


var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "(206) 555-5555",
        address: ['One Microsoft Way', 'Redmond', 'WA'],
        },
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: "(777) 555-6666",
        address: ["One Infinite Loop", "Cupertino", "CA"],
    },
};

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

var search = function(name) {
    for (var key in friends) {
        if(friends[key].firstName === name) {
            console.log(friends[key]);
            return friends[key];
            }
            else {
                return "Nobody by that name."
            }
        }
    };
    
    search("steve");
    console.log(search);


#3

You are searching for a string. you need to delete the " ".


#4

Found the problem. It is not the quotes around "Steve" - removing them returns the error:

ReferenceError: Steve is not defined

The problem was in the "else" portion of the loop. First of all, there should be a semicolon after the return statement, but more importantly return is the wrong call here. It should be:

console.log("Nobody by that name.");

There is no point in returning this string as it is not needed anywhere outside of the immediate output from this loop.

Thanks - I hope this is helpful to someone else.


#5

Cool, I'm happy that you got it to work! :smile:


#6

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