Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Object-Oriented JavaScript (2015) Constructor Functions and Prototypes Create a Constructor

kushalmahajan2
kushalmahajan2
7,683 Points

Need help with task 3 of creating health property

I am doing something like

this.health = 100; this.health--;

and then var monster = new Monster ('helloMonster');

but can't pass the challenge Please help !

monster.js
function Monster(name){
   this.name = name; 
  this.health = 100;
  this.health--;
}

var monster = new Monster('hello');

4 Answers

Hey kushalmahajan2,

If you take Line 4 out of your code, you should pass that challenge. This is the challenge question:

"Finally, Monsters have a health property. When they take damage from the player, their health will reduce. All new Monster instances should have 100 health by default. It should be a number not a string."

You were probably trying to implement the part where it says "When they take damage from the player, their health will reduce." Well, you don't need to do that to pass the challenge but, in any case, your implementation would have been incorrect. Below is one correct way of implementing it. But, remember you don't have to do this at all — if you just take that 4th line out you'll pass the challenge.

function Monster(name) {
  this.name = name;
  this.health = 100;
}

Monster.prototype.reduceHealth = function() {
  this.health--; //Decreases the health by 1, each time it's called
};

Oh, also, you should remove this instantiation:

var monster = new Monster('hello');

The challenge question doesn't ask for it.

kushalmahajan2
kushalmahajan2
7,683 Points

Yes Mikis, I figured this out later. But it does confuses. Thanks for posting !

No problem. Glad you figured it out :)

Samandar Mirzayev
Samandar Mirzayev
11,834 Points

function Monster(name){ this.name = name; this.health = 100; }

Monster.health = function(){ this.health= this.health - this.health;//or this health--; }

var monster = new Monster('hello')

no need long code to write

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

all this question asking is this >>>> Finally, Monsters have a health property.It should be a number not a string {(When they take damage from the player, their health will reduce. All new Monster instances should have 100 health by default) this is some additional info for our understanding}

function Monster(name){
    this.name = name;
    this.health = 100;
}