3/6 whats wrong?


#1

whats wrong with my code ?

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 (contacts) {

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

console.log(contacts[0]);
console.log(contacts[1]);

#2

implement the function this way

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

//now call printPerson but: no need for console.log() since we
//handle that in the function itself
printPerson(mary);
printPerson(bob);

#3

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

this works!


#4

var printPerson = function (contacts)
console.log(bob.firstName + " " + mary.lastName);
var contacts = [0, 1];
console.log(contacts[0]);
:blush:


#5

This worked for me:

function printPerson(){

console.log( bob.firstName, bob.lastName);
console.log( mary.firstName, mary.lastName);

};

printPerson();


#6

And that worked too:

function printPerson(firstName, lastName){

console.log( firstName, lastName);

};

printPerson(bob.firstName, bob.lastName);
printPerson(mary.firstName, mary.lastName);

#7

your code looks interesting, I will try.


#8

Your code didn't work: here what I tried and it worked;

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

printPerson();


#9

whats wrong with this code:

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(mary.firstName + "" + mary.lastName);

};
printPerson(contacts[0]);

printPerson(contacts[1]);


#10

you are not getting the right output all because of this code.

you are only printing mary's credentials. to correct it you have to do

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

};

#11

oh!! thanks man i, yah but it still doesnt work even after adding the second one code:
var contacts = [bob, mary];
// printPerson added here.last
var printPerson = function(person){
console.log(bob.firstName + "" + bob.lastName);
console.log(mary.firstName + "" + mary.lastName);

};
printPerson(contacts[0]);

printPerson(contacts[1]);


#12

when you write the print person function like that then it implies that calling that function will only be limited to bob and mary. but then in reality you could have more than two people.

Now considering the fact that the argument you pass to the function is an object with a first name property then in the function's body you can just do

console.log(person.firstName + " " + person.lastName);

#13

ok,thanks it worked, i really had problems to grasp that,cheers man


#14

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

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

this is how it should look to meet all the tasks in the lesson. sure there are ways to go around with console.log or "printPerson(bob);" instead of printPerson(contacts[0]. I managed to pass the lesson by doing something quite weird, but it let me pass, but i went back and made it the right way. Hope it helps someone :slight_smile:


#15

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(bob.firstName + " " + bob.lastName )
console.log(mary.firstName + " " + mary.lastName )
var printsPerson = [0,1]
console.log(contacts[0].firstName + " " + contacts[0].lastName);
console.log(contacts[1].firstName + " " + contacts[1].lastName);
}

This work too