4. Listing everybody


#1



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


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


#2

@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 ==" );

#3

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,


#5

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


#6

@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
https://discuss.codecademy.com/t/im-having-a-problem-on-lesson-11-23/2307/3?u=leonhard.wettengmx.n

or even better use
= https://discuss.codecademy.com/t/how-do-i-format-code-in-my-posts/28351
[extra's]
https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet


#7

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: