We Made A Friend! Don't know what I'm doing wrong



Hey guys, everytime I try to run my code I get the following error:

SyntaxError: Unexpected token .

I know the problem is related to the line of code where I set

contacts[contacts.length] = {

But I can't for the life of me figure out why this is causing the syntax error. This code seems like it should run to me. I've looked at similar topic answers and my code seems to match up. If someone could point me in the right direction as to what I'm doing wrong it'd be greatly appreciated!

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++) {

/*Create a search function
then call it passing "Jones"*/
function search(lastName) {
    var contactsLength = contacts.length;
    for (i = 0; i < contactsLength; i++) {
        if (contacts[i].lastName === lastName) {
        } else {

function add(firstName,lastName,phoneNumber,email) {
    contacts[contacts.length] = {
    this.firstName = firstName;
    this.lastName = lastName;
    this.phoneNumber = phoneNumber;
    this.email = email;
add("Dave", "Paulson", "(666) 666-6666", "dave@example.com");


There won't be a need for an else branch so that can be removed.

We are not dealing with a constructor functions, but an object literal. Use object literal syntax.

firstName: firstName,


Ah thank you. Got my notations mixed up and that was the root of the problem.

Up till now I thought an if function had to be accompanied by an else function, helpful to know that's not the case. Thanks!


else is a default action. We only ever use it in cases where something has to happen every time, in every case.

var a = ""
var b = ""

if (a) {
   console.log("It's A"};
else if (b) {
   console.log("It's B"};
} else {
   console.log("It's neither A nor B"};

Now change b to a non-empty string, say, b = "B", and run again. Then change a and run again.


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