Please help; Bill Gates is taking over my search function!


#1

Hello all,

I finally wrote a working search function...or so I thought. It passes, but I keep getting a suspicious printout on the console (it used to be Steve's house twice, but I figured I knew the reason for that); now it just keeps printing Bill's info! (One less time if I don't test my search function on STEVE.)

I've tried comparing my program to the example in the introduction and they seem similar to me except for my addition of the toLowerCase function (toLowerCase instead of toLowerCase()). As I wrote this I realized I was missing the parentheses on toLowerCase, but as taking the function away altogether didn't solve my issue, neither did adding in the parentheses.

Without toLowerCase parentheses I get Bill's info about five times (the final one in a more condensed format), with toLowerCaseparentheses the program returns three alternating "not your friend"s with three Steves, one Bill, and one condensed Steve, and without toLowerCase I get three "not your friend"s, two Steves, and one Bill.

Is Microsoft really the best?! Any helpful hints would be greatly appreciated.

frisby


PS: the reason my object is set up the way it is was so I could practice the different notations.
PPS: bonus question: what is being compared if I write name === friends[contact]? Is it comparing the entire contents of said contact to name? Just curious.

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


var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "your mom",
        address: ["Microsoft", "is the best"]
    },
    steve: {}
}

friends.steve.firstName = "Steve";
friends.steve["lastName"] = "Jobs";
friends.steve.number = "Bill's mom";
friends.steve["address"] = ["Bill's mom's house"];


var search = function(name) {
    for (var contact in friends) {
        if (name.toLowerCase() === friends[contact].firstName.toLowerCase()) {
            console.log(friends[contact]);
            return friends[contact];
        } else {
            console.log("This person is not your friend.");
        }
    }
}

search("STEVE");


#2

@msfrisby,
You want to express
`if searched name is NOT found, display 'This person is not your friend.'

You will want this output AFTER you have searched through all of your =data-set=.

var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "your mom",
        address: ["Microsoft", "is the best"]
    },
    steve: {}
};

friends.steve.firstName = "Steve";
friends.steve["lastName"] = "Jobs";
friends.steve.number = "Bill's mom";
friends.steve["address"] = ["Bill's mom's house"];

console.log(friends);
console.log("=======================");

var search = function(lastName) {
    var printed = false;
    for (var prop in friends) {
        if (friends[prop].lastName === lastName) {
            console.log("=== Contact-info for "+lastName+" ===");
            console.log(friends[prop]);
            printed = true;
            break;
        }
    }
    if ( !printed ) {
        console.log(lastName+": No match found");
    }
};

search("Johnson");
search("Steve");
search("Jobs");

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

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


#3

Thank you so much for your help, Leon!

So am I understanding you correctly that if I'd had 10 non-Steve names in my object then my original program would have printed my else condition 10 times in addition to Steve's info?

Thank you also for your "nice to know" tip. May I ask what are the advantages to using the labs page?
At minimum, I figure I don't have to worry about lesson objective-related error messages, but for example are the error messages more descriptive? I've been using repl.it and their messages seem to be the same as the lesson console's.

I also noticed that on the code side, the labs page shows little yellow triangles, although I don't see them in the classroom editor (which tells me to ignore them, although they are not there). What do these mean?

Thank you again,
frisby


#4

@msfrisby,

Your assumption is correct !!.

If you experiment in the labs environment_
you will not be confronted by eventual extra OUTPUT from the =code-checker=

You can HOVER your mouse over the triangles
and you will see a COMMENT.
Try it by using following INVALID code and HOVER over the icon's

console.log("============")
console.log("============);

if ( 1  2 ) {
    console.log("if")
    
else {
    console.log("else")
}

and correct the code following the icon's


#5

Thank you for confirming, @leonhard.wettengmx.n

Ah, I didn't realize it was missing the extra output.

I put in the example you provided as well as my original program and was able to see the messages over the triangles and x's as advised.

Thank you so much again for your time and thorough help, Leon! I really appreciate it on my answer and others'.