6/6 We Made A Friend! Comprehension sake...adding two friends!


#1

I finished this exercise, no problem. But there's one thing that I would like to understand, and perhaps I did not code effectively in the add() function.

In my code below, an object called "axe" is created and later in the same function put inside the "contacts" array. And then I try adding one more contact! And here comes the questions:

  1. Then what comes to my mind is that, doesn't that mean that inside of the contacts array, there's now [bob, mary, axe, axe] inside of it?

  2. Is there anyway to change the name of the object inside of that array? How do I effectively do that for each new addition? (I don't know whether later in the course this is going to be talked about or not...haven't reached there yet)

  3. Btw, I forgot...how do I show the inside of the array? And I mean, printing [bob, mary, axe, axe] to the console? Because when I do console.log (contacts) or use a loop on console.log (contacts [i]), it showed the whole properties of the object

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 search(lastName) {
    var contactsLength = contacts.length;
    for (var i = 0; i < contactsLength; i++) {
        if (lastName === contacts[i].lastName) {
            printPerson(contacts[i]);
        }    
    }
}
/*Create a search function
then call it passing "Jones"*/

//removed for now
//search("Jones")

// add function
function add (firstName, lastName, email, phoneNumber) {
    //create an object called "axe"
    var axe = {
        firstName : firstName,
        lastName : lastName,
        email : email,
        phoneNumber : phoneNumber
        };
    //put the object in the array
    contacts[contacts.length] = axe; 
}

//check the current list
list (contacts);

//adding space
console.log("")
//add the object in as a test
add ("Axe", "Warhol", "axe.warhol@supply.com", "(111)-2223454");

//check if axe is in.
list(contacts);

add ("Andy", "Brown", "asdf@asdf.com", "(222)-1234567")

console.log("")

list(contacts);

console.log(contacts)


#2

@akimbo_dragon,

var myArray =[];
var myObj1 = {name:"Dodo1",value:0};
var myObj2 = {name:"Jojo1",value:0};
myArray =[myObj1,myObj2];
//change the Value of index zero Object
console.log("== change the Value of index zero Object ==");
myArray[0].value = 1;
//Check if the object in the Array is a copy
//or is pointing to the original VARIABLE value
console.log("myObj1.value: "+myObj1.value);
console.log("myArray[0].value: "+myArray[0].value);
console.log("== change the Value of myObj1 variable ==");
myObj1.value = 2;
console.log("myObj1.value: "+myObj1.value);
console.log("myArray[0].value: "+myArray[0].value);
/*
== change the Value of index zero Object ==
myObj1.value: 1
myArray[0].value: 1
== change the Value of myObj1 variable ==
myObj1.value: 2
myArray[0].value: 2
*/
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]);
	}
}

var contacts=[];
function add (firstName, lastName, email, phoneNumber) {
    //create an object called "axe"
    var temp = {
        firstName : firstName,
        lastName : lastName,
        email : email,
        phoneNumber : phoneNumber
        };
    //put the object in the array
    contacts[contacts.length] = temp;
    //create a variable from firstName.toLowerCase
    tmp=firstName.toLowerCase() +"= temp";
    eval(tmp);
}

//check the current list
//list (contacts);

//adding space
console.log("")
//add the object in as a test
add ("Axe", "Warhol", "axe.warhol@supply.com", "(111)-2223454");
console.log("display variable",axe);
//change variable-object
axe.lastName = "WWarhole";

//check if axe is in.
list(contacts);

https://discuss.codecademy.com/t/6-6-difference-with-code/8871/4?u=leonhard.wettengmx.n


#3

No. Variable names cannot be changed, only the values they refer to may be modified.

The objects are not in the array, but where we wrote them. They still exist as unique objects in the namespace. The array entries are only references to the objects.

Try this:

bob.firstName = "Robert";

console.log(contacts[0].firstName);    // Robert

contacts[1].firstName =  "Mary Ann";

console.log(mary.firstName);    // Mary Ann

#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.