Have some difficulties in 6.List 'em all!


#1


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


i can't understand the following instructions, neither the meaning nor the method. Would you please to guide me?
//
In the body of the function, write a for/in loop.
In the loop, use console.log to print out the key. (For example, if you only have bill and steve as entries, list should just print out "bill" and "steve".)//

and the below is the code i write according to my understanding which is somehow correct.
but i can't figure these parts: "var list = function (friends) {
for (var key in friends) {
console.log(key);
var myString = "Awesome!";
console.log(myString);
}"

so javascript knows "keys" means something like property?

var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "(206) 555-5555",
        address: ["One Microsoft Way","Redmond","WA","98052"]
    },
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: "(206) 555-6666",
        address: ["One Apple Way", "Whitemond", "WA", "98052"]
        },
};
    
var list = function (friends) {
    for (var key in friends) {
        console.log(key);
        var myString = "Awesome!";
        console.log(myString);
    }
};


#2

you define key in the loop:

for (var key in friends)

the for in loop does all the work for you, it will assign (in sequence/order) all properties of friends object to key


#3

the for in loop does all the work for you, it will assign (in sequence/order) all properties of friends object to key.

so you mean the key is what i created to store the properties of friends. i misunderstood that js already knew key was the property of object because property can also be called key.

in the instructions , argument is mentioned frequently, what is the meaning of argument??


#4

because you define key in the loop, this could be named anything:

for (var name in friends)

well, yea, JS is confusing in this manner. Because objects are also associative arrays, keys (objects) and properties (associative array) are some used for both. Super confusing...

arguments are supplied when calling the function, to satisfy function parameters


#5

sry i still can't understand the sentence "arguments are supplied when calling the function, to satisfy function parameters",

what is the difference between arguments and parameters.

thank you for your nice help!


#6

a parameter is when you declare a function:

var example = function(parameter){

}

Now you need to provide an argument at function call to satisfy parameter:

example("argument")

#7

oh, can i think argument as something like value to put in the function?


#8

yes, the arguments gets copied into the function parameter:

var example = function(parameter){
      console.log(parameter) // output: argument
}
example("argument")

Maybe revisit some function exercises if you are struggling this much? or visit external sources like MDN - functions


#9

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