6. We Made a Friend


#1

Hi!
First of all sry for my bad English. I don't understand something. It works but the value of the first property (firstName) is [object Object].

Here is the part of my code:

function add(newFirstName, newLastName, newEmail, newPhoneNumber){
    var newFirstName            = new Object();
    newFirstName.firstName      = newFirstName; 
    newFirstName.lastName        = newLastName;
    newFirstName.email             = newEmail;
    newFirstName.phoneNumber    = newPhoneNumber;
    
    contacts[contacts.length]   =newFirstName; 
//This can be wrong, may newFirstName is a string and not an object?
//e.g.:'add("Berta", "Cica", "none", "30 - 44***");'->'contacts[contacts.length]   ="Berta";' and not '=Berta;' ? How can i transform it?
};
add("Berta", "Cica", "none", "30 - 44***");
add("Keki", "Liba", "nonenone", "35 - 79***");
list();

****FULL CODE - so u can try it:D****

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

//search("Jones");

function add(newFirstName, newLastName, newEmail, newPhoneNumber){
    var newFirstName            = new Object();
    newFirstName.firstName      = newFirstName;
    newFirstName.lastName       = newLastName;
    newFirstName.email          = newEmail;
    newFirstName.phoneNumber    = newPhoneNumber;
    
    contacts[contacts.length]   =newFirstName;
};

add("Berta", "Cica", "none", "30 - 44***");
add("Keki", "Liba", "nonenone", "35 - 79***");
list();
console.log(contacts);

THX & marry Xmas
Sir Random Name


#2

I have formatted your code. Your problem:

Hint: You have duplicate variable names for newFirstName (the argument) and the newFirstName (the object you created) in function add.


#3

Sry for the lame format.

Thank you for the answer, I will fix it.

THX & marry Xmas
Sir Random Name


#4

No need for the sorry, just remember to format your code properly next time :smile: