4/6 Building an address book error: Did you remember to call list(); after creating it?


#1

// code:

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);
};

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

list();

//

I see other posts about this but none solve the problem. I keep getting the error "Did you remember to call list(); after creating it?" and I cannot figure it out. Any help would be greatly appreciated!


#2

Your code looks fine. Are you getting the output in the terminal that is expected?

Bob Jones
Mary Johnson

It's worth noting, also, that function declarations and for statements do no end with a semi-colon. Only function expressions do.

var myFunc = function () {
    // anonymous function expression
};

function foo(){
    // function declaration
}
for (var i; ... ) {
    // for statement
}

#3

Thanks, Roy! No, I do not get the expected results. Instead, I keep getting the error "Did you remember to call list(); after creating it?". Any idea what may be wrong? (I've refreshed the browser.) Thanks for the tips re semi-colon! While we're at it, could you explain the (practical) difference between a 'function declaration' and 'function expression'?


#4

A declaration is immutable. The function cannot be removed, modified or renamed. At load time the entire function is hoisted to the top of its scope. It's name is an identifier which can be thought of as intrinsic in the stored object. Declared functions can be written anywhere in the code, above or below the references to them.

A function expression is just that, an expression. The variable is still an identifier, but not intrinsically. It is instead a pointer that can be made to refer to more than one object during the run of the program. The function is said to be anonymous. At load time, only the variable is hoisted to the top of its scope, not the expression (the case with all variables). For this reason, function expressions cannot be referred before they appear in the source listing.


#5

Thanks,again! Any idea why my code is not producing the expected result?


#6

As for advantages, they both have all the properties inherent in a Function object instance, with no differences. Anything we can do with one, we can do with the other (the earlier distinction notwithstanding).

Because they are expressions, anonymous functions can be written anywhere an expression is allowed. Not so with declared functions. They can only appear in global scope, or in function enclosures.


#7

There is the error... L should be l. Sorry about that, I missed it first time through.


#8

Ugh, thank you so much!


#9

Hi joepatrick,

Did you ever in fact get an ANSWER to your error message? I am getting the same error, but the replies don't seem to actually say what is wrong with the code?


#10

#11

Recently challenged and proven to be not the case. Well the linter may flag it, but the compiler allows it. So a function can be unhinged by re-assignment of its name. There are less checks than previously thought.


#14

error was due to contacts.Length; should read contacts.length; ...it's the little things at the Academy


#15

I have input this exact code, with no help and yet it say I didn't log it.