4/6 Listing Everybody


#1

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

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

Here's my code and everything works fine. But can anyone explain why the console is repeating the name twice like so:
Bob Jones
Mary Johnson
Bob Jones
Mary Johnson

Another question: why is the function created like this
function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
}

versus (this is the version that was taught back in Unit 2: Functions). What's the difference?
var list = function() {
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++){
printPerson(contacts[i]);
}
}

To my other question about function syntax, Is one syntax preferred than the other? I know they both worked. Can anyone shed some light? Thanks.


#2

@zirch_o2,

Here's my code and everything works fine. But can anyone explain why the console is repeating the name

Add an extra code line at the End-of-your-code
console.log( "==This is my End==" );

You will see that you have an extra output AFTER your code is excuted
as the course-checker will do it's Display as well