3.Displaying peoplr


#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(person){
console.log(person.firstName+" "+person.lastName);
};
console.log(contacts[0]);
console.log(contacts[1]);
I don't know why this canot work?It says that did log Bob Johnson to the console,somebody can help me?thx


#2

you build a printPerson function, maybe you should call/use the function to print a persons first and last name?


#3

thank you,so did my friend said.but i am weak,i still dont know,it doesn't matter,i need to review the lessons before...


#4

here is some code to help you:

# function declaration
var example = function(x){
    console.log(x);
}

# fucntion call
example("hello world");

x is my function parameter, its a placeholder until you pass an argument when calling the function. I use a string as argument. But this can also be an object (bob, mary) or an array item


#5

it works.but now ....i have some confuse ,I log mary twice,can you show me why.var printPerson=function(person){
console.log(person.firstName+" "+person.lastName);
console.log(contacts[0]);

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

  printPerson(mary);

output
Bob Jones
{ firstName: 'Bob',
lastName: 'Jones',
phoneNumber: '(650) 777-7777',
email: 'bob.jones@example.com' }
Mary Johnson
{ firstName: 'Mary',
lastName: 'Johnson',
phoneNumber: '(650) 888-8888',
email: 'mary.johnson@example.com' }

{ firstName: 'Mary',
lastName: 'Johnson',
phoneNumber: '(650) 888-8888',
email: 'mary.johnson@example.com' }


#6

the advantage of a function is that you can execute a function multiply times by calling the function again, and you can supply different arguments:

# function declaration
var example = function(x){
    console.log(x);
}

# fucntion call
example("hello world");
example("hello again")

This way, your function behaves sightly different.


#7

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