6/6 it's giving me an error " SyntaError: Expected " ; " "


#1

This is my code.

 function add(firstName,lastName,phoneNumber,email){
contacts[contacts.length]{
 firstName: firstName,
 lastName: lastName,
 phoneNumber: phoneNumber,
 email: email,
}
  };
 add("Linda", "Bob" "(112) 222-2222" "lindaBob@example.com");
 list();

It's giving me a SyntaxError Expected ;
Please help. :smiley:


#2

Hi @soon2bcoder

I don't know your full code, but I can show you this (if it could help you):

function add(firstName,lastName,phoneNumber,email){
 this.firstName = "firstName";
 this.lastName = "lastName";
 this.phoneNumber = "phoneNumber";
 this.email = "email";
};

var linda = new add("Linda", "Bob", "(112) 222-2222", "lindaBob@example.com");

console.log(linda);

Hold on :wink:


#3

This is my full 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, add];

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 < contactsLength; i++) {
if(lastName === contacts[i].lastName) {
    printPerson(contacts[i]);
};
};
};
search("Jones")
function add(firstName,lastName,phoneNumber,email){
contacts[contacts.length]{
 firstName: firstName,
 lastName: lastName,
 phoneNumber: phoneNumber,
 email: email,
}
 };
add("Linda", "Bob" "(112) 222-2222" "lindaBob@example.com");
list();

When I tried your code

 function add(firstName,lastName,phoneNumber,email){
  this.firstName = "firstName";
  this.lastName = "lastName";
  this.phoneNumber = "phoneNumber";
  this.email = "email";
 };

 var linda = new add("Linda", "Bob", "(112) 222-2222", "lindaBob@example.com");

 console.log(linda);

it returned

 Bob Jones
 { firstName: 'firstName',
   lastName: 'lastName',
   phoneNumber: 'phoneNumber',
   email: 'email' }

and gave me an error that said " Oops, try again. It looks like you didn't add an person object to the end of your contacts array. "


#4

Ok thanks, I'm going to check


#5

Well, this code works

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(){
for(var i = 0; i < contacts.length; i++){
    printPerson(contacts[i]);
};
};

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

function add(firstName,lastName,phoneNumber,email){
 this.firstName = firstName;
 this.lastName = lastName;
 this.phoneNumber = phoneNumber;
 this.email = email;
 contacts.push(this);
};

var linda = new add("Linda", "Bob", "(112) 222-2222", "lindaBob@example.com");

//TEST WHAT YOU WANT
//printPerson();
//list();
//search("Johnson");
//linda;

Look what I changed and tell me if it's ok :smile:


#6

Thanks so much! :smile: It works now. :smile:


#7

Hi @onepunch_man I had been struggling with this code too, but now the code finally works. But I still don't understand a part in your code.

function add(firstName,lastName,phoneNumber,email){
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.email = email;

contacts.push(this); ----> Could you explain me this line, please?

};

var linda = new add("Linda", "Bob", "(112) 222-2222", "lindaBob@example.com");

Thanks in advance!!


#8

Hi @jonathan2506

Of course, we want to create a new object who called linda, so we called the function add to do it

var linda = new add("Linda", "Bob", "(112) 222-2222", "lindaBob@example.com");

and we want what our function add this new object (after was create) in our array contacts

contacts.push(this);

this represent the object named, here is linda, so linda will be introduce in our contacts arrray.

Is it clear ? Tell me if I must tell it in an other way :sweat_smile:

Note : push() method adds new items to the end of an array


#9

Thank you!! Your explanation is pretty clear. If I had to buy the lunch for you I would do it. :fries: :hamburger: Thanks again :smile:


#10

if(explanationOfOnePunch-Man === clear){
    brain.log("Ok... he wins go around Burger King");
}else{
    brain.log("WTF?!");
};

:smile:


#11

please any help with 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 functionthen call it passing "Jones"*/

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

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

add("ahmed","khattab","00201114845421","ahmed@hotmail.com");
list();

error: Oops, try again. There was a problem with your syntax.
and: SyntaxError: Unexpected token .

i also tried like this:

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

thnaks in advance


#12

Hi @reefoo

Try this :

var add = function(firstName, lastName,phoneNumber,email){
    this.firstName = firstName;
    this.lastName = lastName;
    this.phoneNumber = phoneNumber;
    this.email = email;
    contacts.push(this);
};

var ahmed = new add("Ahmed","khattab",00201114845421,"ahmed@hotmail.com");
list();

Hold on :wink:


#13

ur code is pretty good ,, appreciate that

but also wanted to know my mistake as it is part of learning

can u help plz?
regards


#14

Oh yes of course sorry !

So, this will be replace by the variable name ahmed
And .push() method will put ahmed at the end of your array contacts

var add = function(firstName, lastName,phoneNumber,email){
    this.firstName = firstName;
    this.lastName = lastName;
    this.phoneNumber = phoneNumber;
    this.email = email;
    contacts.push(this);
};

You have to use the keyword new to create an object (with your add constructor) :

var ahmed = new add("Ahmed","khattab",00201114845421,"ahmed@hotmail.com");

#15

when we type:
contacts[contacts.length] = {object}

isn`t this step enough instead of using:
array.push() ?

please explain y do we have to use push, as it is also not recommended at the instruction

regards


#16

You don't have to use .push() method, it's a alternative for your code.

But if you want to create an object with the last index, you can do like this :

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

var ahmed = new add("Ahmed","khattab",00201114845421,"ahmed@hotmail.com");

Do you have questions about this code ?


#17

it gave me this
Oops, try again. Make sure that the list() function still exists! Your code threw the following error when checking if you called list(): Error: Line 53: Unexpected token ILLEGAL

then i called:
list();
still the same error text


#18

here is my last update

i tried to take contacts[contacts.length] outside the constructor
and made it = new Add
and it worked
here it is:

var Add = function(firstName, lastName,phoneNumber,email){
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.email = email;
};
contacts[contacts.length] = new Add ("Ahmed","Khattab","00201114845421","ahmed@hotmail.com");

list();


#19

ur codes make lots of sense too

i was only trying my way

i really appreciate that u replied so fast
and opened my eyes on more optins

best regards