7. Search for a friend - Console showing more names


#1


I'm getting a pass, but why does the console show more names than just the searched "Wendy"?
Also, it shows Wendy, then Steve twice, then Bill, then info on Wendy again but shows it differently.

{ firstName: 'Wendy',
lastName: 'Johnson',
number: '510-555-3333',
address: [ '3555 Anyplace Drive', 'New York', 'NY', '11001' ] }
{ firstName: 'Steve',
lastName: 'Jobs',
number: '408-555-2222',
address: [ 'One Infinite Loop', 'Cupertino', 'CA', '95014' ] }
{ firstName: 'Steve',
lastName: 'Jobs',
number: '408-555-2222',
address: [ 'One Infinite Loop', 'Cupertino', 'CA', '95014' ] }
{ firstName: 'Bill',
lastName: 'Gates',
number: '206-555-1111',
address: [ 'One Microsoft Way', 'Redmond', 'WA', '98052' ] }
{"firstName":"Wendy","lastName":"Johnson","number":"510-555-3333","address":["3555 Anyplace Drive","New York","NY","11001"]}


var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "206-555-1111",
        address: ["One Microsoft Way", "Redmond", "WA", "98052"]
    },  
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: "408-555-2222",
        address: ["One Infinite Loop", "Cupertino", "CA", "95014"]
    },
    
    wendy: {
        firstName: "Wendy",
        lastName: "Johnson",
        number: "510-555-3333",
        address:["3555 Anyplace Drive", "New York", "NY", "11001"]
    }    
};
    
//This prints to the console the "keys" of the object friends, not the firstName properties. Noticed the names are not capitalized.//

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

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

search("Wendy");


#2

Codecademy for whatever reason prints out the result of the last expression or something like that.
You could add a print at the end of your code saying something like "end of output" and then you know you can ignore anything coming after that as your code isn't saying to produce that output.


#3

I went to next part 8.Victory, and they actually placed a line of code:

console.log("end of code");

Tried "end of output", and that works, too.

However, when I go back to lesson 7. Search for a friend, and use console.log("end of code");
it doesn't fix the problem. I'm seeing 4 listings with Bill showing up twice. So, I'm not sure what's going on. Can you or anyone explain this???


#4

This code:

var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "206-555-1111",
        address: [ "One Microsoft Way", "Redmond", "WA", "98052" ]
    },
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: "408-555-2222",
        address: [ "One Infinite Loop", "Cupertino", "CA", "95014" ]
    },
    wendy: {
        firstName: "Wendy",
        lastName: "Johnson",
        number: "510-555-3333",
        address: [ "3555 Anyplace Drive", "New York", "NY", "11001" ]
    }
};

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

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

search("Wendy");

Produces the following output:

{ firstName: 'Wendy',
  lastName: 'Johnson',
  number: '510-555-3333',
  address: [ '3555 Anyplace Drive', 'New York', 'NY', '11001' ] }

The exact formatting probably varies between interpreters since you're asking it to print an object.

So what's showing up twice? Are you talking about some other code?


#5

Thanks. I'm taking a break. I will look at the code you just gave later tonight...carefully.....In case I missed something. The below is what shows in my console, when I added the console.log("end of code"); to 7. Search for a friend.

{ firstName: 'Wendy',
lastName: 'Johnson',
number: '510-555-3333',
address: [ '3555 Anyplace Drive', 'New York', 'NY', '11001' ] }
end of code
{ firstName: 'Steve',
lastName: 'Jobs',
number: '408-555-2222',
address: [ 'One Infinite Loop', 'Cupertino', 'CA', '95014' ] }
{ firstName: 'Steve',
lastName: 'Jobs',
number: '408-555-2222',
address: [ 'One Infinite Loop', 'Cupertino', 'CA', '95014' ] }
{ firstName: 'Bill',
lastName: 'Gates',
number: '206-555-1111',
address: [ 'One Microsoft Way', 'Redmond', 'WA', '98052' ] }


#6

Most of that is after your "end of code"


#7

Geez, totally missed the "end of code" statement after Wendy's info. Thanks so much!!!


#8

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