6/6 We made a friend syntax error


#1

I keep getting a syntax error
SyntaxError: missing ) after argument list

I can't find it and I've been looking for about an hour now. I think I just need a fresh set of eyes. 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"
};
function add(firstName, lastName, phoneNumber, email) {
    contacts[contacts.length] = {
    firstName: firstName,
    lastName: lastName,
    phoneNumber: phoneNumber,
    email: email
    };
};
add("Alex", "Cazz", 222 222-2222, "Alex.Cazz@sample.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]);
	}
}
list();

#2

Hi this line

add("Alex", "Cazz", 222 222-2222, "Alex.Cazz@sample.com")

you should put the phoneNumber inside " " so it should be a string like for mary and bob then you should put the add function at the end of your code then you should also have the search function (I know that the instruction aske you to remove it but you shouldn't because an error will show up about it too )
Hope that could help you.


#3

Hi pac4hz;

Remember your syntax when creating a function, (We declare a function using var followed by the name. Then we use the function keyword to tell the computer that you are making a function
followed by the parameter in parentheses.

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

I agree with wizmarco that you need to make the corrections to your phonenumber string by adding the quotes and you might also want to put parantheses around the area code just to make it the same as bob and mary and also move the add function to the end of your code as it will be neater and it should probably come after the original contacts array. However, I disagree with him about not deleting the search function because I deleted mine and my code ran fine.

I hope this helps.


#4

What he did is perfectly legit. It's just a question of doing a "function declaration" (what he did) or "function expression" (what you're telling him to do). It's a question of wanting the function to be local or global. If you want to be able to use hoisting. Which simply means that you can call a function in the code before it's declared, because of the two phase compiler JavaScript uses.
So it's mostly just a preference to the developer although it gives you more control and oversight by using function expression, because the code isn't scattered around as much.
I just wanted to point that out since it sounds as if you're telling him that what he did was wrong.


#5

u r right and if i recall this was one of the earlier things we learned?


#6

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