4. Listing Everybody



Is this the correct way?

var bob = {
    firstName: "Bob",
    lastName: "Jones",
    phoneNumber: "(650) 777-7777",
    email: "bob.jones@example.com"

var mary = {
    firstName: "Mary",
    lastName: "Johnson",
    phoneNumber: "(650) 888-8888",
    email: "mary.johnson@example.com"

var contacts = [bob, mary];

function printPerson(person) {
    console.log(person.firstName + " " + person.lastName);

var list = function() {
    var contactsLength = [];
    for(i = 0; i < printPerson.length; i++) {




I have some comments on your function list :

1) inspire yourself of the function printPerson to define the name of the function list. You need to manipulate it again.

2) The function printPerson already prints in the console, so you don't need to console.log the output of the function printPerson

3) The for loop must remain general, you should not explicitly write the output for bob and mary. Consider to replace the two lines in the for loop by one line which prints contacts[i].



The list function will iterate the contacts array, which contains references to bob and mary. Since printPerson() is logging, the list need only call that function on each reference.

var contacts = [bob, mary];
// etc.

var list = function () {
    for (var i = 0; i < contacts.length; i++) {


@mtf, normally my (nubly) instinct would be to set the loop as you did here, with i < contacts.length; however the instructions tell us to create a var contactsLength = contacts.length, presumably for this very purpose. Is there a reason why the latter might be a more suitable approach?


There are two schools of thought on this concept. The one school suggests that polling the length property directly in the loop is the way to go; the other school leans toward caching the length property. One would need to investigate this further to draw your own conclusion.

This is the only exercise that will expect the variable, contactsLength. All others leave it up to us.

Some would have us treat .length like a function. As such it would be a pure function which means it can be replaced by the value returned.


y = f(x)

If f(x) = 5, then y = 5. Thus we can replace f(x) with y, or even 5. This is a pure process that will never change.

var array = [1,2,3,4,5,6,7,8,9];
for (var i = 0, n = array.length; i < n; i++) { }

In the new JavaScript (ECMA 2015 -> ES6), n and i are scoped to the loop body only. Once the loop is completed, they get garbage collected. Some would argue that the loop will run faster since the conditional is a simple value, not a construct to retrieve a value from an object property list. Others will argue it makes no difference. Benchmarking uses millions of cycles and times the start to finish of each variation. You may run across some of these in your research on this concept.


Thank you for your help and guidance, @mtf. I tried searching ".length vs caching value javascript" and found a few results. No doubt there's some confirmation bias at work, but after reading I think I'll probably stick with using the property directly for now.

Some would have us treat .length like a function. As such it would be a pure function which means it can be replaced by the value returned.

Does this mean where a declaration is var = f(x) var will always equal the return of f(x), which will run once for the sake of assigning a value to the variable? Just want to make sure I'm understanding this correctly.

n and i are scoped to the loop body only. Once the loop is completed, they get garbage collected

Is this only for certain kinds of loops?
I tried running the program in Codecademy labs with console logs after the loop ends and was able to print i and n; is that because it's not using ES 6 yet?
I'm a bit confused because I read about a "let" variable being introduced in ES 6 in Eloquent JS.

y = f(x)

What f(x) is, is inconsequential. It just is. Some function has been given x and is told to deal with it. y is the outcome, or dependent value.

What we can expect from this relationship is a pure function. Value in, value out. The correspondence never changes. Same value in, same value out.

var y = function (x) {
    return x;

y just became a copy of x. This is known as the identity function, and is fundamental in math and in programming.


@mtf could another function elsewhere change the output of f(x)? Or is it irrelevant regardless because the value out will only be returned once, and assigned to y already?


Please note my edit. My bad...


Just as in math, f(x) is easily incorporated into or relationally bound to g(x).


So sorry, I can't remember the last time I studied f(x) in relation to math...:flushed: Am I understanding correctly that easily incorporated into or relationally bound to g(x) means the value assigned to y will pretty much never change?

I am trying to read up on math and JS identity functions and feel like I'm either over or underthinking this and am stuck in an infinite spiral of incomprehension.

f(x) = x for all elements x in M.[1]
In other words, the function assigns to each element x of M the element x of M. src

var I = function (x) { return x } src

I feel like I'm reading "I am me" a bunch of times and yet also perhaps that that can't be right...


Without driving yourself into a tizzy, give the identity function some thought. Step away and come back a few times. It's actually not that evident what role this function serves, until we progress a little, but it bears understanding now.


Meaning the I combinator?

Thank you, I will...that procedure has come in handy with lot of the stuff I've read during this track.