I am about to explode. Someone please help me


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

function add(firstName, lastName, phoneNumber, email)
{

firstName = firstName,
lastName = lastName,
phoneNumber = phoneNumber,
email = email

}

contacts[contacts.length] = add("info1", "info2", "info3", "info3")

console.log (contacts[2])

Here is the code. Why does it not return "info1", "info2", "info3", "info4"? It only returns as undefined. I've also tried to put "this." before each of the lines of code in the function "add", however that doesn't work. On top of that, I've tried to run the function list aswell, but it returns as "cannot read property 'firstName' of undefined". HELP ME BEFORE I COME TO YOUR HOUSE AND MURDER YOUR FAMILY!!!!

Thank you.


#2

First, calm yourself

Second, check this out :

var add = function(firstName, lastName, email, phoneNumber) {
contacts[contacts.length] = {
firstName: firstName,
lastName: lastName,
phoneNumber: phoneNumber,
email: email
}
};

-then-
add("info1", "info2", "info3", "info3");


#3

Actually, I already figured it out but thanks anyway!


#4

excuse me i want to ask about search function here, this is my code:

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

whats wrong with this? when i call the search function with parameter lastname its not prints person i search for, it prints all contacts instead??


#5

your if condition is incorrect

var search = function(lastName) {
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].lastName === lastName) {
printPerson(contacts[i]);
}
}
};

search("Jones");

let me know if it makes sense