We made a friend: I don't know what's gone wrong[closed]


#1

So I don't know whats gone wrong but this code is not working
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) {
contactLength = contacts.length;
for (var i = 0; i < contactLength; i++) {
if (lastName == contacts[i].lastName) {
printPerson(contacts[i]);
}
}
};

search("Jones");

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

add(11, 1, 111, 1111);
list();


#2

I think you have abrakadabra here:
var this.firstName = firstName;
var this.lastName = lastName;
var this.email = email;
var this.phoneNumber = phoneNumber;
var this.firstname = {
firstName: this.firstName,
lastName: this.lastName,
email: this.email,
phoneNumber: this.phoneNumber
};
Don't use "this"construction.Try new Object:
var add=function(firstName,lastName,email,phoneNumber){
var newOb=new Object();
newOb.firstName=firstName;
newOb.lastName=lastName;
newOb.email=email;
newOb.phoneNumber=phoneNumber;
contacts[contacts.length]=newOb;
console.log( newOb);};