6.) We Made A Friend! I am so rusty


#1

Oh goodness. It has been a long time since I have done a Javascript lesson and it shows lol. Step 2 states, "In this new function, you want to create a new contact object like bob and mary. Instead of having this object's property values be filled with strings though, set them to the appropriate function parameters passed in." However, I am not sure really what this means so I did strings like a bad person. Also, i stink at calling functions for whatever the reason that concept has blown by my head too many times. Also, my error is SyntaxError: Unexpected token { however I believe I have bigger problems with that... puts on cone of shame

Also here is the link to the lesson: https://codecademy.com/courses/building-an-address-book/0/6?curriculum_id=506324b3a7dffd00020bf661#


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

var add = function {
    firstName: "Stetim94"
    lastName: "Bandit"
    email: "kyleaw@appylpye.com
    phoneNumber: "(123) 456-7894" 
    };
var contacts = [bob, mary, stetim94];
add Stetim94


#2

Oh dude since this is a longer code, I'm gonna go check mine, hang in there.


#3

Okay first thing I see is in your function add, it wants you to have parameters firstName, lastName, email, and phoneNumber. (You remember how to add parameters right?)


#4

Then, inside of add, you want to have firstName: and the rest set to things like this:

        firstName: firstName,
        lastName: lastName,
        email: email,
        phoneNumber: phoneNumber

and later assign arguments when you call the function add.

Alright last thing, before you declare firstName and the rest and assign them like I showed you in the code above ^, they all need to be inside of contacts[contacts.length] = {


We Made A Friend! Question
#5

Ok. Well now off to play with that for a moment. Thank you! I will be back with my code shortly.


#6

Okay I'll be around!


#7

Somehow I don't doubt it xD


#8

lets start by saying i feel flattered you added me to your friend list.

Okay, first a problem is here:

var add = function

the parentheses are missing:

var add = function()

which means your parameters are also missing, see step one of the instructions:

Create a function called add with the parameters firstName, lastName, and email, phoneNumber

first make these improvements, then post an updated version of your code


#9

Yeah I am starting to realize this fact. I now understand I got to make this with parameters

function add(firstName, lastName, phoneNumber, email)

I remembered parameters but like I said I be rusty so I had to shake some of that off lol. Second,
`
contacts[contacts.length] = {
firstName : firstName,
lastName : lastName,
phoneNumber : phoneNumber,
email : email
}};

I made the contacts array and I assume the, for example, firstName is great logic coming a place holder it is holding that spot until I add information to firstName later on? Idk maybe lol Now I am working on the add calling.


#10

yea, the parameters serve as placeholders until you call the function, the arguments then serve as values for the placeholders


#11

function add(firstName, lastName, phoneNumber, email) {
    contacts[contacts.length] = {
        firstName : firstName,
        lastName : lastName,
        phoneNumber : phoneNumber,
        email : email
}};
add("Stetim94", "Bandit", "(123) 456-7894", "kyleaw@appylpye.com");

Ok so now I have gotten through I believe the major issues with my last code. However, It looks like you didn't log "Stetim94 Bandit" to the console. Did you remember to call list() after adding your new contact? Do I need to call the contact as well in this case? I don't see what that would be like... I mean your not calling it like add I know that.


#12

your list function:

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

you have to call it again after you called add, to list all contacts (it should show also the new contact)


#13

I'm glad you're getting through it! Also, it's super awesome that you added me as a friend. I feel special. :smile: But yeah everything looks really god, I believe you just need to call list again.


#14

var bob = {
    firstName: "Bob",
    lastName: "Jones",
    phoneNumber: "(650) 777-7777",
    email: "bob.jones@example.com"
};

It's example of object with properties filled with strings values.

You need create function which can add new "friend" to contact array.

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

Now instead of having this object's property values filled with strings, I set them to the appropriate function parameters passed in.

How to use the new function? Just call it and put there string arguments:

add("Denys", "Matsevych", "(111) 222-3333", "my_email@gmail.com");

Also, my error is SyntaxError: Unexpected token { however I believe I have bigger problems with that...

var add = function { // missed (): function()
    firstName: "Stetim94" // missed ,
    lastName: "Bandit", // missed ,
    email: "kyleaw@appylpye.com // missed " and ,
    phoneNumber: "(123) 456-7894" 
    };

Also. problem here:

var search = function(lastName) {
   var contactsLength = [bob, mary] // should be: var contactsLength = contacts.length or remove this line if used contacts.length in for loop
   for(var i = 0; i < (contacts.length); i++) {
      if(contacts[i].lastName === lastName) {
         printPerson(contacts[i]); 
      }
   } 
}

Also here:

var contacts = [bob, mary, stetim94];

You forgot create stetim94 variable. But you don't need to do that because you have function add() which do this work without var contacts = [bob, mary, stetim94];

Should be smth like this:

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 < contactsLength; i++) {
		printPerson(contacts[i]);
	}
}

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

var add = function() {
	contacts[contacts.length] = {
		firstName: "Stetim94",
		lastName: "Bandit",
		email: "kyleaw@appylpye.com",
		phoneNumber: "(123) 456-7894" 
	}
};

add("Denys", "Matsevych", "(111) 222-3333", "my_mail@gmail.com");
list();

#15

Thank you man. That well explained and I appreciate the lengthy reply. I think that is a good overall explanation of what I did wrong!


#16

Awesome, how many posts you got when I writing my answer! )))


#17

add("Stetim94", "Bandit", "(123) 456-7894", "kyleaw@appylpye.com");
list();

Is it really that simple though? I mean i know that is calling list but idk I guess I was expecting something harder lol.


#18

Haha. Yeah it was several xD But i still appreciate it. I think having a lengthy reply covering everything can be really useful for people just wanting to see everything explained in one place.


#19

@kyleaw, @denys.matsevych, @stetim94 thank you all for your help! I was able to pass the lesson with flying colors! This is why I love forums. Well, atleast good forums like this one xD I really appreciate all the time explaining and helping me! Gosh, I don't know where I be in my coding without stuff like this. I probably be finding { syntax errors in Rock Paper Scissors haha! However, I have no idea who to give the solution mark to...


#20

thx that helped :smiley: