I'm working on a simple Address book. 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);
}
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"*/
function search(lastName) {
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength;i++) {
if(contacts = person.Lastname) {
printPerson(contacts[i]);
}
}
};
search("jones");
#---------------------------
I'm thinking the problem with my code is the search function. For the search function, I want to get a person in my contacts by calling his last name, and receiving only that person from my contacts. Why is there a problem with this code? If you could, please help be debug this code. Thank you for your time!
[Re-posted
WHy does this happen? Js[
^] — SA]