4. Listing everybody (This is strange...)

function list() {
var contactsLength = 2;
for (i = 0; i < 3; i++) {
console.log(printPerson);
}
console.log(list)
}

This was my first attempt to this one and this is what I wrote, I was surprised that it said I passed but then I looked at the console and this is what it wrote:

Bob Jones
Mary Johnson
[Function: printPerson]
[Function: printPerson]
[Function: printPerson]
[Function: list]

this looked very strange to me and I’m not sure if I did it wrong…

I changed it to this but this time it prints out the names 3 times

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

list();

Nevermind, I solved this one too!

What was the solution?

Hello!
I have a question for smart people :slight_smile:
explain me, why code academy asks me to do this:
“At the start of the function, define a variable to store the number of items in the contacts array. Call it contactsLength”.
why we need this var here?
maybe it could work without contactsLength?
function list () {
for (i = 0; i < contacts.length; ++i) {
printPerson(contacts[i])
}
}

4 Likes

Kinda sure that they wanted us to use it in the for loop instead of using contacts.length. Took me time but thats what I think.
Am I “SMART” now? ^3^

1 Like

It was very confusing! I have know idea what “Instructions” was talking about! Why making something so simple so hard to understand???

I wrote this and it was fine:
function list() { for (i = 0; i < contacts.length; i++) { printPerson(contacts[i]); } }

3 Likes

Probably )) will check it after I get some more experience in JS ))

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

The reason they ask us to define the variable is they want us to use it in the for loop instead of the .length method. We just use contactsLength instead.