This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-05-10 20:34:14

ImagineIt
Scratcher
Registered: 2011-02-28
Posts: 1000+

JS Code Not Working?

I tried this code in the codecademy labratories and it keeps saying "NaN damage!" What's up with it? Thanks!

Code:

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

 

#2 2012-05-10 21:23:51

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: JS Code Not Working?

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

 

#3 2012-05-10 22:16:35

jvvg
Scratcher
Registered: 2008-03-26
Posts: 1000+

Re: JS Code Not Working?

I know some JS, but I'm not good with creating classes.

NaN may mean wrong data type (like trying to do math with a string).


http://tiny.cc/zwgbewhttp://tiny.cc/e1gbewhttp://tiny.cc/zygbewhttp://tiny.cc/izgbew
Goodbye, Scratch 1.4  sad                                                        Hello Scratch 2.0!  smile

Offline

 

#4 2012-05-11 01:37:54

ManaUser
Scratcher
Registered: 2009-03-11
Posts: 100+

Re: JS Code Not Working?

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!");

?


http://i.imgur.com/SPYSM.gif http://i.imgur.com/t9k1Z.gif http://i.imgur.com/OwYVa.gif http://i.imgur.com/0qlZq.gif

Offline

 

#5 2012-05-11 02:27:18

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: JS Code Not Working?

ManaUser wrote:

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 "+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:

Code:

console.log("The enemy took " + String(this.oldHealth - this.health) + " damage!");

Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#6 2012-05-11 16:05:37

ImagineIt
Scratcher
Registered: 2011-02-28
Posts: 1000+

Re: JS Code Not Working?

No, it's not this.oldHealth because oldHealth is a parameter.

Offline

 

#7 2012-05-11 16:34:06

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: JS Code Not Working?

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...?


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#8 2012-05-11 16:36:47

ManaUser
Scratcher
Registered: 2009-03-11
Posts: 100+

Re: JS Code Not Working?

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)


http://i.imgur.com/SPYSM.gif http://i.imgur.com/t9k1Z.gif http://i.imgur.com/OwYVa.gif http://i.imgur.com/0qlZq.gif

Offline

 

#9 2012-05-11 17:14:43

rookwood101
Scratcher
Registered: 2011-07-29
Posts: 500+

Re: JS Code Not Working?

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.


http://i.imgur.com/zeIZW.png

Offline

 

#10 2012-05-13 08:12:28

ImagineIt
Scratcher
Registered: 2011-02-28
Posts: 1000+

Re: JS Code Not Working?

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

 

Board footer