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 trial

JavaScript

Why doesn't this work?

I'm pretty new to constructor functions, so I realize I probably am being stupid with this, but in both of the constructors' damage prototypes, I have the character's health being decreased by the other character's attackPower, but I get NaN, why?

window.onload = function() {

  function Neptunian(attackPower, health) {

  this.attackPower = attackPower;
  this.health = health;

  }

  function Seren(attackPower, health) {

    this.attackPower = attackPower;
    this.health = health;

  }

  Neptunian.prototype.damage = function() {

    this.health -= Seren.attackPower;

    return this.health;
  }

   Seren.prototype.damage = function() {

      this.health -= Neptunian.attackPower;

      return this.health;
    }

let seren = new Seren(150, 200);

let neptunian = new Neptunian(50, 100);

  neptunian.damage();

if (neptunian.health == 0) {
  document.write("You won!");
}

if (seren.health == 0) {
  docuument.write("You lost!");
}

}

2 Answers

Steven Parker
Steven Parker
243,656 Points

The "attackPower" is an instance property, but your code is trying to access it as if it were a class variable. You might want to pass the opponent doing the damage as an argument to allow access to the correct value:

  Neptunian.prototype.damage = function(opponent) {
    this.health -= opponent.attackPower;
    return this.health;
  }

This would also make the function identical for both types, so you could optionally make it a function of a common prototype they both inherit from.

Thank you Steven, that worked perfectly, but I do have one more question. Now in my program I have these four lines of code where "neptunian.damage()" used to be:

seren.damage(neptunian);
seren.damage(neptunian);
seren.damage(neptunian);
seren.damage(neptunian);

Now direct your attention to the attackPower of the neptunian and the health of seren. Since subtracting 50 from 200 four times would get you 0, I am confused as to why the final if statement does not run when I execute this code.

Steven Parker
Steven Parker
243,656 Points

I don't think there's enough information here for me to understand the issue. Are you saying the first 3 statements all execute but the 4th does not? I can't see how that would happen.

Also, I'm confused: if the original statement was "neptunian.damage()", so wouldn't the replacement be "neptunian.damage(seren)"? It looks like you're inflicting damage on the wrong party here. And why replace one with four?

It might help to make a workspace snapshot (upload to workspace if needed) and share the link to it.

I think you're overthinking it a little lol. I'm not trying to do anything specific, I just want to see what happens when I do different things, but I am confused with seren.damage(neptunian); four times not resulting in a "You lost!" message

Steven Parker
Steven Parker
243,656 Points

Oh, I see. In that case, I'd guess you just haven't fixed the spelling of "docuument" (with an extra "u") yet.

Omg thank you I'm an idiot lol. I was wondering why 'You won!' worked and 'You lost!' didn't