Why do I have to input the same property values multiple times?


#1



https://www.codecademy.com/courses/building-an-address-book/0/6#


The code below passes. However, I don't understand why, in the "add" function, I need to rewrite all the new properties of the new contact INSTEAD of just writing the name of the new object. It seems like I'm doing the same work multiple times.
In other words:
1. In line 37, why can't I write only :
function add() {
instead of writing out all the parameter names, which I need to do anyway inside the function brackets?
2. After defining the new person (var sarah), why can't I write only:
add(sarah);
instead of writing add (ALL THE PARAMETER VALUES THAT I WROTE DOWN IN THE LINES BEFORE)? The correct code does not make the work any easier --- the opposite, IMHO.
That's why I don't understand the logic. Perhaps someone can please explain it to me.


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[i].lastName === lastName) {
            printPerson(contacts[i]);
        };
    };
}
    function add(firstName, lastName, phoneNumber, email) {
        contacts[contacts.length] = {
            firstName: firstName,
            lastName: lastName,
            phoneNumber: phoneNumber,
            email: email
        };
    }
    var sarah = {
        firstName: "Sarah",
        lastName: "Rose",
        phoneNumber: "(650) 999-9999",
        email: "sarah.rose@example.com"
    }
    add("Sarah", "Rose", "(650) 999-9999", "sarah.rose@example.com");
    list(contacts);


#2

i don't understand why you have this:

    var sarah = {
        firstName: "Sarah",
        lastName: "Rose",
        phoneNumber: "(650) 999-9999",
        email: "sarah.rose@example.com"
    }

it doesn't really serve a purpose.

Now you have your add function, you need one line:

add("Sarah", "Rose", "(650) 999-9999", "sarah.rose@example.com");

which if you add need to add multiply people, is very useful


#3

Hi. Thanks for responding so quickly.
At the beginning, we defined "bob" and "mary" with all the properties.
Using this logic, it seems that I would have to define "sarah" also. It seems to me that the "correct code" is adding all the properties but there is no "sarah" defined in the array.
So later, if I want to specifically call a specific property of "sarah", it will never accept "sarah.lastName", for example. I must know its index number. Otherwise, I would not be able to call it.
Sorry, but this is my logic...


#4

yes we did, but then we build a function (add) to make creating new objects easier. So that creating new objects can be done in one line:

add("Sarah", "Rose", "(650) 999-9999", "sarah.rose@example.com");

#5

I understand.
I guess everyone's logic is different. Personally, I feel it's strange that I have a contact list array which includes a few names at the beginning, but any additions afterwards are defined only by index number. It seems that it would complicate efforts afterwards to call the information of a specific contact.
Unless that's the topic of a future lesson :grin:

Thank you very much for explaining.
David


#6

what do you mean by this? The array is super useful to loop over all your contacts.


#7

Never mind. I'm a technical writer who's learning some programming skills. I'm not a professional programmer, so I don't necessarily think like one.
The array is very useful...it's just that this lesson asked us to create something that IMHO, is inconsistent. But let's carry on. You answered my question. I'm not thrilled with the method, but I accept the reality of it.
Thanks very much!


#8

But do you understand what the lesson was trying to teach you?

If you are programming outside the lesson, you are free to choice whatever method you prefer, but the lessons are designed to teach multiply methods, so you know which methods you are able to pick. You don't have to like all of them


#9

Yes, I understood. Thank you very much.
In a day or two, I'll continue through the course. :sunglasses: