Question of Introduction to Object 1 (Building an Address Book Q4) - Thanks ! I finish this exercise and sleep Thx!


#1

I cannot understand "Have the function run printPerson on the person".
I believe I have use an if statement to check whether lastName argument === lastName of the object (contact[i].contactsLength).

But, the function cannot run and I have no ideas !!! Thanks :slight_smile:

Actually, the problem should be at the bottom.

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

/*Create a search function
then call it passing "Jones"*/
var search = function(lastName){
var contactsLength = contacts.length;
for(var i=0; i < contactsLength; i++){
if(contactsLength[i].lastName === lastName){
printPerson(contacts[i]);
}
}
};

search("Jones");


#2

You can't use here contactsLength because it is a number value which equals to contacts's array length. Here you need to use array contacts:

if(contacts[i].lastName === lastName)

#3

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