3.Displaying people


#1


I don't understand why I can't make code like that.The Error -

  Oops, try again.
Did you create a function called printPerson?


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



but if I change my code for :



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);
    }// printPerson added here
printPerson(contacts[0]);
printPerson(contacts[1]);


This code is OK to pass to the next level. Why ? Both of them is OK !    
Where is the problem in the first version of the code?


#2

Yes, both codes are ok just the assignment wants you to create
function printPerson(){} this kind of function that is not saved into a variable.


#3

In the top of my message I comment a part of code ( // function printPerson (person){ ) and then console.log(contacts[person].firstName+" "+contacts[person].lastName);
}
printPerson(0);
printPerson(1);

It's not good code to pass this level.


#4

Oh I see. Yeah the output is the same from both examples just the thing is that challenge parser that checks your code has it own tests that passes your code and determinate if it is good on his standards.
That parser is written by people as an example of checking if you have done it right.
Things like this happen and parser only accepts 1 answer but that doesn't mean that the other code is wrong like in this case.


#5

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