6. List Em' All!


#1

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

I have a quick question regarding the code above. It passes, and I understand the concept of a placeholder word (the exercise gives the example of "key" in the for loop). However, I don't understand how the function knows I want just the first names listed. For example, if I exchange firstName in the for loop above with lastName, number, or address, it still prints firstName. If I wanted to run a for loop on the object friends and access a different property (such as lastName), how would I do that?

Thanks very much!


#2

You're assuming that it prints the first names. Be careful with that, only formulate questions based on what you know or you may not find any answers.

To refine your question a little, you're wondering what values are assigned to your variable firstName as you loop through an object.
To find out, look up documentation for the for-in statement. Visit your favourite search engine and search for "mdn for in" which brings you to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in

Also don't forget to carefully read the instructions to determine what the code is actually supposed to be doing. It wasn't printing first names. You gotta figure out the intention first, so that you're chasing the right behaviour.


#3

Oh, so the exercise was just to print all of the keys (not the first names) of object friends?

I was confused because exercise 3 states: "Each friend will need a name, phone number, and so on. We will use a new object for each friend so that we can remember their information! That's right, we'll have objects within objects!"

I was under the impression that steve, bill, etc. were new object names (per lesson 3) and I was supposed to access THEIR keys and values. In this exercise, it seems like steve: { (and other names) are keys themselves (with additional keys and values).


#4

Variables are names referring to values, but variables aren't part of the values they refer to. Values don't have names. They're stored somewhere in memory and can be referred to. If you create a value and want to have access to it a little later, then you would store a reference to it so that you can find it again.


#5

Thanks for that explanation. The way variables are accessed changes, though, depending on what the variable is, correct? The way I would access data in a "for loop" would be different than how I access a key in an object (I think). I guess I just need to memorize all the ways to access different types of variables.


#6

You can juggle those references around just about any way you can imagine. The scheme for adding and removing references is a big part of what defines a data structure. Some schemes keep values sorted at all times, others have constant-time look-up time. Values might be linked (references) with each other with varying number of links between them or there might be some index separate from the values. Or both. Dealing with all that is just a matter of lining up the right actions to get the desired result. Syntax for doing so is rather insignificant, figure out what you want to do and then just look up how to do it if it's not obvious at that point.

The most basic data structure is probably array, in languages closer to the machine the location of the array is the location of the first element, and then you add to that address to get the nth value. Because memory supports random access, look-up time is constant for finding the nth value. On the other hand, if you need to inspect each value, then you'd have to iterate through each one, that's no longer constant but takes time relative to the number of values in that array.


#7

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