Object || lesson 2 fun with functions


#1

please help

function Person(job, married) {
this.job = job;
this.married = married;
// add a "speak" method to Person!
this.speak = function(){
console.log("hello");}
};

var user = new Person("Codecademy Student",false);
user.speak();

error: Oops, try again. It looks like 'Hello!' wasn't logged to the console. Make sure that you properly defined the method and that you didn't change any of the provided code.


#2

You printed "hello" instead of "Hello!". Slight mistake there :smiley:


#3

Oh
Thank u very much.


#4

and whats wrong with this code plz

var james = {
job: "programmer",
married: false
};

// set to the first property name of "james"
var aProperty = james["job"];

// print the value of the first property of "james"
// using the variable "aProperty"

console.log(aProperty);

error Oops, try again. Do you have the variable 'aProperty' between brackets?


#5

You put this as a string:

Because it's a key/property of james, you aren't supposed to put "job", but just job.:smiley:


#6

i thought so too

but it doesnt even work with both ways


#7

Okay, first, I found out this is lesson 5, not lesson 2. Second of all, you did a little flaw over there, now that I see what you're supposed to do. The problem is that you're printing

james["job"]

Which is a problem. What you're supposed to do is name aProperty as "job".

var aProperty = "job"

Now, you can't just print aProperty, for that'll just print "job". So you have to say that it's the property of james, like this:

james[aProperty]

Hope it helped!:smiley:


#8

perfect answer and explanation

thanks a lot
:smile: