3/6 what is wrong with my code?


#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];

// printPerson added here
var printPerson = function(i){
console.log(contacts[i].firstName + " " + contacts[i].lastName);
};
printPerson(0);
printPerson(1);

it says i did not create a printPerson function


#2

There's your problem. You've got an "i" in between your function parentheses. Try taking it out.


#3

Well, actually I haven't gotten there yet but it looks fishy to me. Am I right, people?


#4

if i take it out, it says i did not define "i" :pensive:


#5

Well, what is i for? I might be able to figure this out...


#6

Define the printPerson function this way

function printPerson(i){
console.log(contacts[i].firstName + " " + contacts[i].lastName);
}

but notice that this function takes in a person object so all you have to print the attributes of that person like this

function printPerson(person) {
    console.log(person.firstName + " " + person.lastName);
}

#7

Oh! That makes sense. You see, I'm not really experienced and I haven't even gotten to this section yet, but I try to help where I can. It's just I've never seen something in the function parentheses before, so I thought it was fishy...


#8

"i "means a number. it similar to that one in a for loop. most of people use person to call the function, but i use the order in "contacts" to call the function


#9

Well, try @rydan's response.


#10

Also, while I'm here, could you look at my post "No ideas"? It's in the Code Your Own Adventure 2 section. Thx


#11

@jrpsychgal if you have programmed any other languages before then a function here is analogous to methods in other languages. functions can have no arguments or a set of arguments that you have to pass. You will come across it in the tutorials


#12

thanks rydan, but it dosen't work ,either. so if i name a function with the word person, it means i must use person as the paremeter? dose the name affect the choice of the paremeter inside ()?


#13

Well, JavaScript is actually my second language, if you count HTML and CSS... But I'm really a beginner in actual programming.


#14

thanks man, welcome to share your ideas, it dosen't matter if it is correct or not, we will improve together


#15

Well, maybe Codecademy wants you to use person instead of contacts.


#16

The name does not affect it but the implementationin the function changes a little


#17

so it means my way works actually, but the practice asks me to do another thing? i just noticed the hint said i should use person as the arguement


#18

Hey, um could someone look at my post "No ideas"? I haven't got any answers and my brain is a blank.


#19

using person as the argument just tells the user ohh hey this function expects a person object to be passed as an argument any other argument and the function will not work


#20

This post is old, but I ran into the same issue because I initially coded it the same way you did and I wanted to respond for future reference. The function should work as you wrote it, but you won't be able to move on to the next step.

We want the argument to be contacts at an index rather than just the index.

So your function should be

function printPerson(person) {
    console.log(person.firstName + " " + person.lastName);
};

and your calls should be

printPerson(contacts[0]);
printPerson(contacts[1]);