Made a new friend HELP!


#1

Error says "Syntax error. Missing before statement"

Here's 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 contactsLength2 = contacts.length;

for (var i = 0; i < contactsLength2; i++) {
    if (lastName == contacts[i].lastName) {
        printPerson(contacts[i]);  
    };
};

};


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

 add("Sam", "Watts", "swatts@me.net", "312 555 1212");  
 list();

Thanks.


#2

@maychild,
Referring to the code of yor POST
you have 3 problems
-1
If you use commenting text
you will want ot use the line-commenting like

//Create a search function
//then call it passing "Jones"

-2
In your list function
you have a FOR-loop condition

i <= contactsLength

which should be

i < contactsLength

-3
In your add function
you are the using the var keyword

var contacts[contacts.length] = {
}

which would cause a =new= contacts-Array to be generated
and as it does not yet exists a contacts.length will cause an error !!!


#3

WOWY!!

After so much cutting and pasting it's easy to wreck the whole thing.
Note to self, WATCH IT!

Thank you so much!

Here's my working code for anyone else struggling with this monstrosity.

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 contactsLength2 = contacts.length;

for (var i = 0; i < contactsLength2; i++) {
if (lastName == contacts[i].lastName) {
printPerson(contacts[i]);
};
};

};

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

add("Sam", "Watts", "swatts@me.net", "312 555 1212");
list();