Victory! if/else statement question


#1


The result of the code is:
First Name: Bill
who?

But I'm wondering why it hits "who?" in the else statement. Since we searched Bill and it returned, shouldn't it stop the code from hitting the else part of the statement?

I know I can just remove the else statement since it's not necessary here, just wondering why this is the case.


var friends = {}
friends.bill = {
    firstName: "Bill",
    lastName: "Bozo",
    number: "20",
    address:["7610", "Pineville Circle","California"]
};
friends.steve = {
    firstName: "Steve",
    lastName: "Wong",
    number: "30",
    address:["333", "Orange Circle","Washington"]

};
var list = function(){
    for(var X in friends){
        console.log(X)
    }
}
var search = function(name){
    for(var keys in friends){
        if(name ===friends[keys].firstName){
            console.log("First Name:"+" "+friends[keys].firstName)
        }
        else{
            console.log("who?")
        }
    }
}
search("Bill")


#2

I was wondering the same too. I think the console will log some other values in it.

For myself, I removed the else statement and sent the console.log("StringHere") outside the for loop.


#3

Hi there.

envyennui is alluding to something here.

Check out your for loop. It's going through each person in the friends list. It goes through each person and compares their first name with "Bill". The first person it finds is Bill. So the if condition evaluates to true and prints out "First Name: Bill"

But wait, there's nothing stopping the loop. It did it's job and it's going to do it again, this time for steve. Steve is not "Bill" so it prints "who?"

You could prevent the loop from running after it finds a match in many ways. A simple solution would be to place break; just below console.log("First Name:"+" "+friends[keys].firstName). Break will break out of the loop.

You could also instead just return a string. Returning the string will both break out of the loop but also prevent the rest of the function from executing. You'd need to print the string outside of the function somehow though either by directly including the function call in a console.log() or by assigning the return result to a variable and then printing it out.

Hope that clears it up.


#4

Thanks! that does help, but one question, since I entered Bill as the argument for the function, search("Bill"), shouldn't it only search for Bill?


#5

Nope. Even though you specified just "Bill", the logic in your function is telling a different story. Your function is basically just looping through each of the friends objects ie friends.bill and friends.steve. When it finds a matching first name for "Bill" it will print "First Name: Bill", for any other case it will print "who?". This is why you get the result you got. Try adding another friend under steve with the same first name as Billy. Have a look for yourself as to what will happen. .


#6

Ah I get it now, so it's this code (above) that's causing it, since the for loop hits every property of the object.


#7

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