Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialkushalmahajan2
7,683 PointsNeed 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 !
function Monster(name){
this.name = name;
this.health = 100;
this.health--;
}
var monster = new Monster('hello');
4 Answers
miikis
44,957 PointsHey 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
7,683 PointsYes Mikis, I figured this out later. But it does confuses. Thanks for posting !
miikis
44,957 PointsNo problem. Glad you figured it out :)
Samandar Mirzayev
11,834 Pointsfunction 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
13,912 Pointsall 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;
}