4. Listing Everybody - Names Print Twice and Can't Figure Out Issue


#1


I have managed to fix a lot of issues with the code, based on reading through another post about this and the solutions that were offered. I've taken out all the extra code CodeAcademy tells me to write that is unnecessary, but I still have the names Bob And Mary printing out twice to the console, like this:

Bob Jones
Mary Johnson
Bob Jones
Mary Johnson

I can't figure out why it prints twice. Please help me find the issue.


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

list();


#2

The full output of running your code is:

Bob Jones
Mary Johnson

Anything else is caused by codecademy's environment


#3

Nice, so it's not my code. Thanks!


#4

You could add an additional print at the end of your code to mark the end of your own output.
You could also run it in a different environment, directly in your browser, or some online interpreter thing, or nodejs - to name a few environments you could use


#5

The instructions asks for a variable called contactsLength. Try defining a variable within your function called 'contactsLength' that is the length of the contact array.


#6

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