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


#1

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....


#2

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


#3

Nevermind, I solved this one too!


#4

What was the solution?


#5

Hello!
I have a question for smart people :slightly_smiling:
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])
}
}


#6

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^


#7

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


#8

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


#9
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.