Click here to Skip to main content
15,912,457 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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]
Posted
Updated 27-Feb-15 18:33pm
v3
Comments
UWStudents 25-Feb-15 8:28am    
Ooooo, in the "if" statement, did I have to do: if(contacts[i].lastName = lastName) { }??
Sergey Alexandrovich Kryukov 28-Feb-15 0:33am    
Please keep in mind that re-posting is considered as abuse, by apparent reasons.
—SA
UWStudents 28-Feb-15 7:36am    
But it is a different problem! The other one has different code in it!

1 solution

You totally ignored my answer: WHy does this happen? Js[^].

You did not comment it, did not ask anything, you just ignored it. And reproduced the same "programming crime" as it was in your previous question. But before you fix it from the very beginning, there is nothing to talk about. You approach is wrong from the very beginning and need to be fixed.

—SA
 
Share this answer
 
Comments
UWStudents 28-Feb-15 7:37am    
What do you mean by reposting? Is it the question?
Sergey Alexandrovich Kryukov 28-Feb-15 7:39am    
Posting same question again. Did you understand my answer?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900