4. Listing Everybody


#1


https://www.codecademy.com/en/courses/building-an-address-book/0/4?curriculum_id=506324b3a7dffd00020bf661


Hello everyone, I can't find what's wrong with my code? It shows me SyntaxError: Unexpected token ) in the console.
And it tells me "Oops, try again. There was a problem with your syntax."

So, here's my code below, can anyone help me, please? Thanks!


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

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

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


#2

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

You have some errors in your for Statement

for ([initialization]; [condition]; [final-expression])

Please use semicolons.

AND

You made a Array called contacts

Here you wanna log contact

printPerson(contact[i]);

#3

Thanks a lot!! It works. I saw the error of "contact" and "contacts" after the post. But yes, we must use semicolons in the for loop. Thanks again


#4

## answe:

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


#5

Thanks! Problem solved. :slight_smile:


#6

Can someone explain to me why we needed to create another variable called contactsLength to store the number of items in the contacts array? Why couldn't we skip that part and jump right into a for loop?


#7

I also need to know why. But I think we can skip this step and write directly "contacts.length".


#8

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