-1

I'm new to coding, and this is my first post. Any help you can offer is greatly appreciated. I'm trying to create a level calculation system that you might see in an RPG for a character with different stats. When I run the program at different values, I get the following message:

Unexpected token 'this'

I have put the section of code where the error message occurs in stars.

const Sean = {
    fullName: `Sean`,
    attack: 1,
    strength: 1,
    defense: 38,
    hitpoints: 40,
    prayer: 15,
    range: 98,
    magic: 99,
    calcCombatLevel: function () {
        this.Meleelvl = .25 * (this.defense + this.hitpoints + Math.floor((this.prayer / 2))) + ((13 / 40) * (this.attack + this.strength));

        this.magiclvl = .25 * (this.defense + this.hitpoints + Math.floor((this.prayer / 2))) + ((13 / 40) * (this.magic * (3 / 2)));

        this.rangelvl = 0.25 * (this.defense + this.hitpoints + Math.floor((this.prayer / 2))) + ((13 / 40) * (this.range * (3 / 2)));


        if ((this.attack + this.strength) > ((2 / 3) * this.magic) && (this.attack + this.strength) > ((2 / 3) * this.range)) {
            return this.meleelvl;
        }
       // **********************************
       else if ((2 / 3) this.magic > (this.attack + this.strength) && (this.magic >** this.range)) {
            return this.magiclvl;
        }
        else if((2 / 3) * this.range > (this.attack + this.strength) && (this.range > this.magic)) {
        return this.rangelvl;
            }
        }
    }
Sean.calcCombatLevel();
console.log(Math.floor(Sean.lvl));
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • You need an asterisk after the (2 / 3) for your multiplication. – Arya McCarthy Aug 03 '21 at 12:52
  • 1
    Which programming language is this? Please [edit] to clarify, and add tags. – tripleee Aug 03 '21 at 12:52
  • Thank you for the help!!! I put in the asterisk, but now any values I put in I end up with the message not a number when I run the program. I am using Javascript – Sean Kennedy Aug 03 '21 at 12:54
  • Please post that as a separate question, and include the full traceback. Right now, I don’t see where you ever initialize `Sean.lvl`. – Arya McCarthy Aug 03 '21 at 13:00
  • Thank you to all of you who posted. I realize I didn't ask the best question, and I will better follow the forum guidelines in the future. Arya McCarthy, you were right, I was missing the asterisk after (2/3) – Sean Kennedy Aug 03 '21 at 13:28

1 Answers1

0

The first elseif has:

(this.magic >** this.range)
// also missing * at start, has (2/3) magic, where the 'this' error is from

which I assume you meant to be:

else if ((2 / 3) * this.magic > (this.attack + this.strength) && (this.magic > this.range)) {

Didn't play with the code but that's the only first obvious thing that stands out to me.

Also as someone commented above Sean.lvl doesn't seem to be defined, so the output at end probably isn't working either. You could do something like the following instead (or define a field named lvl):

var mylvl = Math.floor(Sean.calcCombatLevel());
console.log(mylvl);

And at least in one place your variable names have inconsistent casing, this.Meleelvl and this.meleelvl, fix that too (first line in function should have this.meleelvl, keeping all variable names lowercase).

Steve Williams
  • 128
  • 1
  • 7
  • Hi Steve. Thanks for your help. The problem was with the casing I used for my variable names. When I fixed the casing, the program ran correctly. I also had to do what you and others said and define the variable Sean.lvl – Sean Kennedy Aug 03 '21 at 13:34