4/6 Listing Everybody, But the function hasn't been called yet


#1

I completed this particular exercise, but now I just need a little bit of clarification. I have only one question.

The last instruction in this exercise asked us to call the "list" function, and so I did that as per below, and it showed the results twice.

When I removed it, the results of Bob Jones and Mary Johnson are shown in the console just like it's supposed to be instead of the double printing.

Urm...is there a glitch in the system or something?

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 (i = 0; i < contactsLength; i++) {
        printPerson (contacts[i]);
        }
    };
    
list ();


#2

};<---remove semicolon

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

list ();


#3

I tried it...but there's no change in the results. It still double prints when I call list (); at the end like the code below.

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 (i = 0; i < contactsLength; i++) {
        printPerson (contacts[i]);
        }
    }

list ();

#4

hello @akimbo_dragon

for (i = 0; i < contactsLength; i++) ----> I think you should declare i into a var :slight_smile:


#5

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