Made a Friend(((What's wrong? SyntaxError: Unexpected token [


#1

Need help! Please, find my mistake! There is 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]);
}
};

var search = function (lastName) {
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) {
if (lastName===contacts[i].lastName){
printPerson(contacts[i]);
};

};

search ("Jones");

var add=function (firstName,lastName,phoneNumber,email) {
var contacts[contacts.length] = {
firstName: firstName,
lastName: lastName,
phoneNumber: phoneNumber,
email: email
};
};
add("Nina", "Richi", "8888-8888", "bbbb@jjjj.com");
list();

console returns SyntaxError: Unexpected token [


#2

there are two problems, the first is in your search:

var search = function (lastName) { <- this opening bracket doesnt have a matching closing bracket
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) {
	if (lastName===contacts[i].lastName){
	printPerson(contacts[i]);
	};

};

the second problem is i think a variable scope, in this line:

var contacts[contacts.length] = {

remove the var keyword, using the var key word makes the array have a local scope, which doesn't seem to be good enough


#3

Thanks a lot!!!! It works!!! =))