Unrecognized second method?


#1

Hi,

I'm getting the same results that are required by the exercise, but I'm doing it in a slightly different way to what successfully completes the exercise. I have:

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

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

printPerson(0);
printPerson(1);

Is there anything wrong with doing it this way? I've changed my code to what it should be and completed the exercise, but I'm just curious as to whether there's anything wrong with my approach.

Thanks,
S


#2

Your code finishes the job.

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

Variable name can be changed into

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

to make it more readable. ^^