4. Listing Everybody


#1

Here is my code:

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

I get the error, that "person" is undefined. This same code worked for me in the previous section, but it's not working here. I changed "person" to "contacts" and I get the same error. What's wrong with it?


#2

When you do the for loop over an array then you have to remember that array elements start with index of 0.
Since your contacts.length will return 2 the for loop will try to look for the third element.


#3

got it, that makes sense


#4

So how did you fix it?

I assume it is something here?

(var i = 0; i<= contactsLength; i++)

#5

It is the right place yes, there is two ways to fix it.

  1. Changing the i is less or equal than to less than.

(var i = 0; i < contactsLength; i++)
  1. Taking contactsLength and subtracking 1 from the total length, because arrays start from 0

(var i = 0; i <= contactsLength - 1; i++)

#6

Here is a code that worked for me;

function list(){

var contactsLength = contacts; 
for( var i = 0; i< contactsLength.length; i++){

    console.log(printPerson(contactsLength[i]));
    }
};

list();

#7

for me it works with this code but not sure if that is correct?

function list(){

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

list();


#8

So i have a question on why it asks you to create a contactsLength var? I ran the code without just using contacts.length in my for statement and it worked just fine.


#9

You have added 2 .lengths in your code.


#10

It's just a practise and to keep a consistent line of teaching. The shortest would be to use contacts.length inside a for loop, but it may confuse the fresh programmers brain


#11

Here is my code

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

list();

The question this prints out

Bob Jones
Mary Johnson
Bob Jones
Mary Johnson

Why it print out twice ?


#12

I found the Problem its because the last list (); prints Bob Jones, Mary Johnson
and Print Person function also does same.
so that is that.

The code must be without last """" list();""""


#13

This is also what I used, except I just used "i < 2" (since, in this particular case, there are only 2 contacts) instead of "i < contactsLength" (BTW, you don't need .length after contactsLength since we already set " var contactsLength = contacts.length" in the previous exercise):

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

list();

#14

I have managed to pass but not satisfied with what has been printed out:

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

My result:
Bob Jones
undefined
Mary Johnson
undefined
Bob Jones
undefined
Mary Johnson
undefined

Can someone please explain this to me?

Thank you in adavance!


#15

It's a glitch on codeacademy's site. The code runs fine in the browser console.


#16

I am having a problem with my code, I think it might be a codecademy glitch, and because of it I can't pass the course

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;
    var (for i=0; i< contactsLength; i++){
        console.log(printPerson(contacts[i]));
    };
}
list();

SyntaxError: missing variable name, Is all that is given. All the time, all the time


#17

try this:

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


#18

IT still doesn't work, with the same Syntax error. I think this is a glitch


#19

in your for "i<= contactsLength" replace to "i<= contactsLength-1" or "i< contactsLength"


#20

Don't think you guys need the console.log() in the console.log(printPerson(contacts[i]) as it's redundant. The purpose of the printPerson function is already to console.log. That could be why it is appearing twice. You're basically logging what you've already told it to log.