6.We Made a Friend!


#1

Help me debug it,i don't know what is wrong with 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"*/
var search = function (lastName){
    var contactsLength = contacts.length;
    for(i=0; i<2; i++){
        if(contacts[i].lastName==lastName){
            printPerson(contacts[i]);
        }
    };
};

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

add("rays","bones","ray@example.com","167326839");

list();

**



**


#2

I don't see the above way of adding a new object as "syntaxly" correct and not the solution to the instruction.

How about you prompt the user for firstName and so on, and then the add function does the addition to contact. Like:

firstName: prompt("What is your firstName:", firstName),

Then, you will not obviously need to call the add function this way.

Just doing:

add();

will do.

See instructions 2 and 3 in the exercise,please.

Thank you! :slight_smile:


#3

when do i need to use [ this. ],this syntax?


#4

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