4. listing everybody


#1

So the task is:

1.Create a function called list that does not take any parameters.
2. At the start of the function, define a variable to store the number of items in the contacts array. Call it contactsLength.
3. All of the items in an array are numbered, starting at 0. To cycle through all of the elements of the array, create a for loop that cycles from 0 up to one less than the number of items in the contacts array.
4. Inside of the loop, add code to call printPerson, passing in the element of the array that the loop is currently at.
5.At the very bottom of the file, call the list function. The list function should then loop through every member of the contacts array and print its information.

And 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; // I don't understand the point of this function, we could create a for loop without it.
for (i = 0, i < contacts.length, i++) {
console.log(printPerson[i])
}
};

list();

Explain please, why do we need to create the list function instead of just console.loging everything with for loop?
why do we need a var contactsLength = contacts.length? To me it looks like var butter = butter


#2

The point of that variable is to create a for loop that iterates over all of the contents of that contacts array. How would you write this for loop if you didn't know the length (or size) of the array?


#3

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

why wouldn't it work?


#4

Technically it would work but it's generally a good idea to extract that number in a different variable and then use it (The way it is done in this exercise). You're not always going to have access to the original variable and often times you work in a totally different scope, so it's easier to just carry around the variable that you need contactsLength than having to carry around the whole contacts object, which could be enormously big.


4. Listing Everything
#5

Alright, this makes sense


#6

The content of your console.log is incomplete as it excludes the name of the array holding the contacts. Try: console.log(printPerson(contacts[i]));


#7

Thanks a lot man, now i understand. But i'm still getting this stuff:
"SyntaxError: Unexpected token )"


#8

Well, I don't see a problem with the parentheses, but you should separate your sections of the for loop with a semi colon, not a comma:

Right way:

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

Also add a 'var' there. :smile:


#9

Now everything works fine, thanks man :wink:


#10

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++){
console.log(printPerson[i]);
};
};

list();

Does anyone have an idea about what's wrong with this code? It keeps returning this message:

Oops, try again.
It looks like your list function didn't console.log "Bob Jones". Did you remember to call list(); after creating it?

--> From what I can tell it matches the code posted in this forum. Any ideas? Thanks


#11

Never mind, I figured it out.


#12

I would recommend removing the console.log portion of that line and keeping:
printPerson(contacts[i]);


#14

i have tried with console.log(printPerson[i]), but it didn't work...printPerson(contacts[i]) is working. Thank you! here is my code:

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