4. Listing everybody

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/building-an-address-book/0/4?curriculum_id=506324b3a7dffd00020bf661#

<In what way does your code behave incorrectly? Include ALL error messages.>
There is no error message coming up and it technically does what it is supposed to, but it seems to call it twice?? In the console i get :

Bob Jones
Mary Johnson
Bob Jones
Mary Johnson

```

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

list();

<do not remove the three backticks above>

@lynettebarnes,
In some sections of this course, the course-checker Display’s data
this you could then identify by placing two EXTRA code-line’s:
console.log( “== My End ==” );
console.log( “== All that is displayed now, is the course-checker ==” );

Thus use

list()
console.log( "== My End ==" );
console.log( "== All that is displayed now, is the course-checker ==" );

Hi,

In your for-loop, you are using “contacts.length” instead of “contactsLength”. Try using the last one to see what happens. It worked fine for me.

Harsh,

I think its because when in your List function, you are telling the function to execute the printPerson function, which includes printing the persons name. Then, you call the List function and it does it again.

It seems Codecademy does this often for some reason.

Also, I don’t understand the purpose of making a contactsLength variable. completely unnecessary step in my opinion.

The code works if you just don’t make the contactsLength variable OR call the ‘list’ function

1 Like

@digoperroni

Please re-edit your Post

  • leave one blank-line above of your code
  • select your code in the Post
  • then =click= on the </>-symbol-of-this-editor

Your code will then be in a pre-code state
and you will be able to make/present the proper indentations.

With Courtesy to stetim94
Im having a problem on lesson 11/23

or even better use
= How do I format code in my posts?
[extra’s]
https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet

I was wondering how to explain your fail (declare a variable “contactsLength” you arent using in your loop), and I just searched that your code can be just simplifier:

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

…and it works :slight_smile: