Don't understand point of placeholder parameter


#1


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


This code passes, but I have no idea what the two parameters that I called "placeHolder" and "placeHolder2" mean. What do they do? Does their name really not matter? Could anyone suggest a more meaningful name for them that might help me visualize their purpose better?


var friends = {};

friends.bill = {
    firstName: "Bill",
    lastName: "Gates",
    number: "555-402-5555",
    address: ["One Microsoft Way", "Redmond", "WA", "98052"]
};

friends.steve = {
    firstName: "Steve",
    lastName: "Jobs",
    number: "555-843-5555",
    address: ["12 Your Mom's Place", "Anytown", "AZ", "78112"]
};

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

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


#2

Hi
Does their name really not matter?
theu name doesn't matter you can give them any name you want
Could anyone suggest a more meaningful name for them that might help me visualize their purpose better?
it up to you to chose what name you want to use there isn't a right chose so ...for me I often use item.
or dor example for the list function i'll use friend and in the search function ill use item.. you could use any you think will be beter for you..


#3

Thanks for the response, wizmarco! Your example names for the placeholder helped me understand what role they're playing in this code... I think I've got it now.


#4

Hello Marco,
I have additional question.. so I do not get the placeholder either.
specifically, the for loop is looping in friends objects and how?
what "placeholder" is accessing in the friends object? when we print from console.log(placeHolder);
why is it only printing Bill and Steve, the first name?
Sorry if my questions are not clear.


#5

HI
the for loop is looping in friends objects and how?
you can read how it work here for in
you have an object family

var family = {

};

the inside it you have some people...

var family = {
    fred : {
         name: "Fred",
         age: 0,
    },
    ben:{
         name: "Ben",
         age: 6,
    }
};

and you want to print the objects fred and ben(which are inisde the object familly) you will do it with the for in like that

for (var item in family) {
    console.log(item)
}

#Output 
fred
ben

why is it only printing Bill and Steve, the first name?
it's because of the condition you put in the if statement(inside the search function)

if (friends[placeHolder2].firstName === name)

this part

friends[placeHolder2].firstName

says to take the objects inside the friends object(both steve and bill)... and to select the firstName of Bill and Steve(both) because of the placeholder2 work like the placehoder it loop through the object and it first take bill then steve firstname


#6

Hi johnnykoo, if I understood it correctly from the previous answer, the "placeholder" here is fulfilling the same purpose as the variable "i" did in previous exercises in this course. For instance, in for loops:

for (i = 0; i < array.length; i++)

So, the variable (whether it's "placeholder" or "i" or anything else) represents each item within the array, taken in turn. (I hope this explanation is right!)

P.S. And I guess this means I didn't actually have to use two separate variables, but could have just used "i" in both cases...


#7

Thank you so much for your complete tutorial. It was more than enough.
Hope you have a great weekend!
Johnny


#8

Thanks a lot! Now I get it.


#9

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