6. We Made a Friend (Completely Lost!)


#1


I am completely stumped on this exercise. I've tried to read a few other examples, and now I am even more confused, because I've tried to add in what I am missing, but some people are using new things like contacts(newPush) and I have no idea what that is, because it was not something presented in this lesson.

Using the code presented in this lesson, can someone with patience please tell me what I am doing wrong? I know this is very off...

Thanks A Million in Advance!


My only error message is:
SyntaxError: Unexpected token :


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 < contacts.length; i++) {
        if (contacts[i].lastName === lastName) {
            printPerson(contacts[i]);
        }
        else {
        };
    };
};
        search ("Jones");
    
    function add(firstName, lastName, email, phoneNumber) {    
    firstName: firstName,
    lastName: lastName,
    email: email,
    phoneNumber: phoneNumber
    };
    
    contacts[contacts.length] = new add("Liv", "Tyler", "LT@gmail.com", "(424) 852-5471")

    list();


#2

@alliec
1.the problem was in the function "add" it self you need to define an object and to assign the values for the properties ... and then to add that object to the list .... (inside the add function)
2.uder the add function ... just call it and pass the values to add func.
i hope that piece of code could help you ...

  function add(firstName, lastName, email, phoneNumber)
    {    
//here you define object called person
    var person={property: value,      
    lastName:property2:value};
//you add the object person at the end of the list
    contacts[contacts.length] = person;
    };
  // you send the arguments to the function   
    add("Mat", "Ranegie", "ranegie@gmail.com", "(909) 820-4628")

i hope that piece of code could help you ...


#4

Sadly this code still did not work for me. I kept getting the same syntax error message with " : "

But I did it a different way (following another post) and got it right.

Thank you though!


#5

i hope you've understand :slight_smile:


#6

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