What's wrong with this code?(3/6)


#1

https://www.codecademy.com/courses/building-an-address-book/0/3?curriculum_id=506324b3a7dffd00020bf661

my code is ..

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(person)
{
    console.log(this.firstName+" "+this.lastName);
};
printPerson(bob);
printPerson(mary);

and i am getting errors.


#2

Use "contacts" instead of "person" which you haven't defined.

var printPerson = function (contacts)

Change this.xxx to contacts.xxx:

{
    console.log(contacts.firstName+" "+contacts.lastName);
};

Don't use printPerson to call your variables 'mary' and 'bob', but call your array 'contacts'. Remember: arrays start at 0.

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

This should solve your problem.
Have fun coding!


#3

@batraaf
i know that using contacts instead of this solves the problem, but i want to know, why can't we use this here?


#4

you could certainly use this if you declare the function in a different way

var printPerson = function() {
   console.log(this.firstName + " " + this.lastName);
}

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

check out this page that explains what values 'this' contains in different contexts: https://javascriptweblog.wordpress.com/2010/08/30/understanding-javascripts-this/


#5

But in this case you'd have to make sure that bob and mary have such a printPerson property and that this function is assigned to it. Otherwise it will just tell you that undefined is not a function.

But still you got the point this. is only possible to be used if you're inside of an object. So that you can say I want the xxx property of this object.


#6

well, could you tell me how it worked here(compare it with the original code)

var setAge = function (newAge) {
  this.age = newAge;
};
// now we make bob
var bob = new Object();
bob.age = 30;
bob.setAge = setAge;
  
// make susan here, and first give her an age of 25
var susan=
{
    "age":25
};
susan.setAge=setAge;
susan.setAge(35);
// here, update Susan's age to 35 using the method

#7

so according to you, it could have worked if i wrote something like..

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 contacts.printPerson=function(person)
{
    console.log(this.firstName+" "+this.lastName);
};
printPerson(bob);
printPerson(mary);

i already tried it, but it didn't work either.


#8

It is probably quite non-standard to give an array additional properties, but it can be done. Once we sort out the syntax errors above, we get,

contacts.printPerson = function(person)
{
    console.log(this.firstName+" "+this.lastName);
};
contacts.printPerson(bob);
contacts.printPerson(mary);

which returns,

undefined undefined
undefined undefined

But we won't give up yet. Consider the following:

contacts.printPerson = function(x) {
    console.log(this[x].firstName+" "+this[x].lastName);
};
contacts.printPerson(0);
contacts.printPerson(1);

which returns,

Bob Jones
Mary Johnson