I tried this code in the codecademy labratories and it keeps saying "NaN damage!" What's up with it? Thanks!
var Player = new Object();
Player.maxHealth = 100;
Player.health = 100;
Player.strength = 15;
Player.speed = 20;
function Enemy(maxHealth, strength, speed){
this.maxHealth = maxHealth;
this.strength = strength;
this.speed = speed;
var Enemy.loseHealth = function(healthLost){
this.oldHealth = this.Health;
if (Math.floor(Math.random()*5-1) != 5){
this.health = this.health - healthLost;
if(this.health < 0){
this.health = 0;
}
console.log("The enemy took "+oldHealth-this.health+" damage!");
if(health === 0){
console.log("The enemy was knocked out! The player won!");
}
} else{
console.log("The attack missed!");
}
};
}
var Enemy1 = new Enemy(100, 15, 19);
Enemy1.loseHealth = loseHealth;
Enemy1.loseHealth(12);Last edited by ImagineIt (2012-05-11 16:15:40)
Offline
I indented it a bit so it's easier to read. By the way, I don't know a bit of JavaScript, so I'm afraid I can't help you.
ImagineIt wrote:
I tried this code in the codecademy labratories and it keeps saying "NaN damage!" What's up with it? Thanks!
Code:
Player.maxHealth = 100; Player.health = 100; Player.strength = 15; Player.speed = 20; function Enemy(maxHealth, strength, speed) { this.maxHealth = maxHealth; this.strength = strength; this.speed = speed; var loseHealth = function(healthLost) { this.oldHealth = this.Health; if (Math.floor(Math.random()*5-1) != 5) { this.health = this.health - healthLost; if(this.health < 0) { this.health = 0; } console.log("The enemy took "+oldHealth-this.health+" damage!"); if(health === 0) { console.log("The enemy was knocked out! The player won!"); } } else { console.log("The attack missed!"); } }; } var Enemy1 = new Enemy(100, 15, 19); Enemy1.loseHealth = loseHealth; loseHealth(12);
Last edited by amcerbu (2012-05-10 21:24:30)
Offline
Forgive me if I'm completely off target, because I really don't know what I'm doing but...
Shouldn't this
console.log("The enemy took "+oldHealth-this.health+" damage!");
be
console.log("The enemy took "+this.oldHealth-this.health+" damage!");
?
Offline
ManaUser wrote:
Forgive me if I'm completely off target, because I really don't know what I'm doing but...
Shouldn't thisconsole.log("The enemy took "+StringoldHealth-this.health+" damage!");
be
console.log("The enemy took "+this.oldHealth-this.health+" damage!");
?
I think you're right; it should.
To solve the NaN errors, you could try using String:
console.log("The enemy took " + String(this.oldHealth - this.health) + " damage!");Offline
No, it's not this.oldHealth because oldHealth is a parameter.
Offline
ImagineIt wrote:
No, it's not this.oldHealth because oldHealth is a parameter.
But you haven't used oldHealth as a parameter anywhere... and you used "this.oldHealth" in line 14...?
Offline
Then how about where you used this.oldHealth several lines earlier? As far as I can tell they're in the same scope, so I'd have thought they should match.
Edit: Ninja'd!
Last edited by ManaUser (2012-05-11 16:38:09)
Offline
In the two lines at the end:
var Enemy1 = new Enemy(100, 15, 19); Enemy1.loseHealth = loseHealth;
First of all, I can't see where loseHealth is defined and second of all, why are you setting Enemy1.loseHealth to that variable anyway? Enemy1.loseHealth is a function, assigning it a value would just lose the function.
Offline
rookwood101 wrote:
In the two lines at the end:
Code:
var Enemy1 = new Enemy(100, 15, 19); Enemy1.loseHealth = loseHealth;First of all, I can't see where loseHealth is defined and second of all, why are you setting Enemy1.loseHealth to that variable anyway? Enemy1.loseHealth is a function, assigning it a value would just lose the function.
Enemy.loseHealth is a method, but it says that "." is undefined for some reason...
Offline