5/6 Exercise Help


#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() {
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 (contacts[i].lastName === lastName) {
printPerson(contacts[i])
}
}
};

search("Jones");

/////////////////////////////////////

I do not understand where my code went wrong.


#2

Look at your code:

here is your function

function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
}

and you called it here

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

but you forgot to call list function .without calling it'll not work.so you have to call it.like

list(); call the function

so now it'll print

Bob Jones
Mary Johnson

and look at your search function

var contactsLength = contacts.Length; // here

write contacts.length not contacts.Length //JavaScript is a case-sensitive


#3

Thanks rcodeman for the help


#4

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