6. we made a friend!


#1


I am stuck on this task and i cant figure it out

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

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

add ("Jake", "Coffey", "Jakecoffey2004@gmail.com", 123 4567 890)
list();

}

syntaxerror: unexpected token

Replace this line with your code.


#2

Hi!

Take a look at this area specifically.

Let me know if you see it, if not I can elaborate on it. I would rather you try to figure it out first now that you know where the problem lies specifically.


#3

Is it because of the semicolons. I'll have a closer look at it tomorrow. Do they need to be commas?


#4

hi,

@lloan I've just solved the mistery:

  1. The order of the arguments in your function add don't correspond to the order of the arguments in a line where you call the function.
  2. You should use a hyphens in a phone number.
  3. And semicolons, obviously :wink: Change it to commas. @fieryguitarist

Thank you for the material to think on!


#5

... and the : instead of = in the object!


#6

@kryscina @fieryguitarist I'm glad you guys took another look at the issue.

Take a look at this:

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

So the function add() will just serve the purpose of creating a new object and adding it to the contacts array. We pass it in 4 parameters that will be given by a person ( first name, last name, email and phone number). For the new object we are creating, we are setting those values to the input passed in to the function. I labeled the new object my add() function creates 'newEntry' just so I know what it is.

After you fix that, you have two other issues in your code.

Within your call to the add() function, the telephone number has an error, make it a string and instead call it as so:

add ("Jake", "Coffey", "Jakecoffey2004@gmail.com", "123 4567 890");

Also, don't forget the semi-colon at the end of your call to the add() function.

At the very end you are adding a curly bracket. Remove that.

Let me know how that works for you guys - I'm happy you guys went back to try and figure it out. :slight_smile:


#7

thank you so much! it worked. it just needed a bit of editing and switching around. although, it logged "Bob Jones" to the console twice. do you think that from previously in the course? also, while i am at it i will tell you about a glitch i have encountered with codecademy. sometimes, the @ and " keys switch around when im writing code with codecademy. i haven't encountered it with any other program and i have to change keyboards temporarily to fix it. do you know why this is?


#9

To add a new object directly into the array position without even giving it a name, this is my code : -
function add(firstName, lastName, phoneNumber, email){
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.email = email;
}
contacts[contacts.length] = new add("Tom", "Li", "(444) 555-6666", "tom.li@example.com");

list();


#11

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