Address book (Needs debugging)


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

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


#2

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

1.As youre not using contacts.length; then you can delete it.

2.Your for loop..

for(i=0; i<contacts.length; i++); <-- delete this semicolon
also when you define iteration variable inside for loop dont forget to use var keyword.
ex..
var i = 0

3.Whatever you put inside the for loop,put them inside the curly brackets(A good practice)

4.this line of code..

printPerson[i];

i is an index number but you want to access mary and bob?
that can be accessed using contacts and passing it index i ?
You need to pass it to printPerson() function (note: function always take arguments inside the parentheses not square bracket)


#3

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

Here's my updated code.


#4

good work!

printPerson(i);

Understand it..
when you pass i to it,You're passing 0 and 1 (as length of contacts is 2)
But you have to access bob and mary..

contacts[0] is bob
contacts[1] is mary

so you can see that we have to pass index ( i) to contacts Array,then we have pass it to printPerson() so it can print name of corresponding items of contacts


#5

Thanks! You've been a great help!


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.