We Made a Friend 6/6 Problem!


#1

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 add = function (firstName, lastName, email, phoneNumber) {
var contactsLength = contacts.length;
var contacts[contactsLength] = {
firstName = firstName,
lastName = lastName,
email = email,
phoneNumber = phoneNumber
};
add("Rob", "Benson", "rob@jack.com", "777-3444");
list();

Here's my code.

The error I'm getting is:
SyntaxError: Unexpected token '['. Expected ';' after var declaration.

WHY?

Thanks.


#2

Also, a sidebar question here:

Why do we need the variable "var contactsLength = contacts.length;" to store contacts.length in. Why can't contacts.length just stand on its own?


#3

function add(firstName,lastName,email,phoneNumber)
{
//this will add new object to end of contacts
contacts[contacts.length] ={
firstName: firstName,
lastName: lastName,
email: email,
phoneNumber: phoneNumber //took out comma here
} ;
//take this line out
//contacts.push(contacts[contacts.length]);
}
add("Rob","Balmer","person@ex.com","(999) 888-8888");
list();

This is some corrected code from someone else. I ran the code and it worked, but I still don't see the difference between this code and my previous code :frowning:


#4

Hi veryscarycary,

I got stuck at the same point, after some long time of reflection I got it :wink:
The difference between your code and the one you found ist the decleration of contacts[contacts.length] as variable, I should not be done as we are adding an object and not a variable.

About your sidebar question: you don't need the variable "var contactsLength = contacts.length;" it works perfectly fine without.

Hope it helped!


#5

In regards to the contacts.length, its not needed, codecadamey has a habit of attempting to make cleaner code by assigning a new variable when using a method, even if you'll only use it once.


#6

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

This is how it needs to look like. No need for your extra var contactsLength = contacts.length;
And this part of code that I just gave you, you can find in example in that exercise you are doing.

contacts[contacts.length] = {
    firstName: firstName,
    lastName: lastName,
    phoneNumber: phoneNumber,
    email: email
};

this part is in exercise.


#7

Thanks everyone! It helped a lot!


#8

Hi! I tried to copy and paste your message into the workspace, but this is the response...

TypeError: undefined is not an object (evaluating 'contacts[i].lastName')

Is there something wrong with the bottom part?

```add ("Steve", "Jobs", "stevejobs@abc.com", "246812343579")
list();
```

#9

After having news eyes on this post after a few days, it looks like I was missing a closing curly bracket } near the end. There are two opening {'s after var add = function... and var contacts[contact.length].... but only one closing } that I can see. That's probably where the problem is.


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

}

in my code there are two opening brackets and two closing brackets.


#11

I'm not sure I can help you. I've since been cramming Ruby lessons into my mind. I'm preparing for a test in Ruby so my javascript knowledge is probably far from desirable at this point. =/


#12

that's no problem. I've recently finished HTML & CSS, jQuery and Java courses, so it's no biggie. Looking forward to your reply!


#13

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"*/
function search(lastName){
contactsLength = contacts.length;
for(var i= 0; i < contactsLength; i++) {
if (contacts[i].lastName == lastName){
printPerson(contacts[i]);
}
}
}

search(contacts["Jones"].lastName);

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

}
}

add("jon","smith","777-666-8888","yesbae@me.com");
list();

Here's my code.

The error I'm getting is:

TypeError: Cannot read property 'lastName' of undefined

Why ??


#14

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

};
search("Jones");

var add=function(firstName,lastName,email,phoneNumber){
var newName={
firstName: "firstName",
lastName:"lastName",
phoneNumber:phoneNumber,
email:email
};

contacts[contacts.length]=newName;
};


add("Bob", "Johnson",  "(650) 7977-7777",  "bob.jones@example.com");
list();