1

I am learning about objects and their scopes, I got confused and decided to make a D&D pathfinder character sheet for practice.

I made the character sheet as an object, child objects for ability scores, strength/dexterity/intelligence/etc... and then base and modifier values.

in D&D you use your base value to calculate your modifier value and the modifier is used throughout the game. The calculation for modifier is (base - 10) / 2

as i run this i only get 0 when called console.log(characterSheet.abilityScore.strength.modifier)

I assume my confusion is about my understanding of the scope range between the function and modifier/base. but I'm not sure where to start.

const characterSheet = {
  abilityScore: {
    strength: {
      base: 2,
      modifier: 0,
      calcMod: function () {
        this.modifier = Math.floor((this.base - 10) / 2);
      },
    },
    dexterity: {
      base: 8,
      modifier: 0,
      calcMod: function () {
        this.modifier = Math.floor((this.base - 10) / 2);
      },
    },
    intelligence: {
      base: 16,
      modifier: 0,
      calcMod: function () {
        this.modifier = Math.floor((this.base - 10) / 2);
      },
    },
  },
}

I have also tried placing the function in the global scope but always returns NaN.

const characterSheet = {
  abilityScore: {
    strength: {
      base: 2,
      modifier: function () {
        return calcMod(this.base);
      },
    strength: {
      base: 8,
      modifier: function () {
        return calcMod(this.base);
      },
    },
    strength: {
      base: 16,
      modifier: function () {
        return calcMod(this.base);
      },
    },
  },
}};
function calcMod(val) {
  return Math.floor((val - 10) / 2);
}

console.log(calcMod(characterSheet.abilityScore.strength));
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
mastaSabin
  • 131
  • 1
  • 7
  • 3
    Add the code that calls `calcMod()` – Barmar Sep 29 '22 at 00:35
  • 2
    After I call `characterSheet.abilityScore.strength.calcMod()`, when I log `characterSheet.abilityScore.strength.modifier` it shows `-4`. – Barmar Sep 29 '22 at 00:38
  • @MisterJojo I was referencing for when I make a function outside the object. – mastaSabin Sep 29 '22 at 01:05
  • @Barmar am I building this wrong then? I don't want to call the function every time I want to call the modifier. wouldn't that be repeating myself? – mastaSabin Sep 29 '22 at 01:08
  • @mastaSabin Please [edit] your question to show the code where you make the function "outside the object". – Bergi Sep 29 '22 at 01:10
  • 1
    @mastaSabin If you don't want to repeat yourself, use one function that builds these objects: `{strength: buildScore(2), dexterity: buildScore(8), intelligence: buildScore(16)}` – Bergi Sep 29 '22 at 01:12
  • @MisterJojo updated to include function outside of the object. also, do you mean pathfinder? It's D&D 3.6 basically – mastaSabin Sep 29 '22 at 01:26
  • I don't know what is `D&D 3.6` is there a link which can explain this stuff? – Mister Jojo Sep 29 '22 at 01:29
  • @MisterJojo Doungens and Dragons https://dnd.wizards.com though I don't know if pathfinder is part of the Wizards company or not. I am referencing https://www.d20pfsrd.com/ for most of my info about pathfinder. – mastaSabin Sep 29 '22 at 01:31
  • 2
    change `console.log(calcMod(characterSheet.abilityScore.strength));` to `console.log(calcMod(characterSheet.abilityScore.strength.base));` – Mister Jojo Sep 29 '22 at 01:40
  • ok, D&D stands for Dungeons and Dragons... I hardly play any games (I didn't even know what a sudoku was recently), next time try to conceive that everyone does not gravitate in the same universe as yours ;) – Mister Jojo Sep 29 '22 at 01:48
  • @MisterJojo I was mostly trying to keep the details related to JavaScript. – mastaSabin Sep 29 '22 at 02:36
  • @Bergi how could I build this function? when I use it as you suggest it works as expected. but when i place modifier: `buildScore(this.base)` it returns NaN. – mastaSabin Sep 29 '22 at 02:46
  • Yes, you are supposed to use it as I suggested. It's still not clear what other way you want? – Bergi Sep 29 '22 at 02:49

3 Answers3

1

you can use that... ?

const characterSheet =
  { abilityScore: 
    { strength:
      { base      : 2
      , modifier  : 0
      }
    , dexterity: 
      { base      : 8
      , modifier  : 0
      }
    , intelligence: 
      { base      : 16
      , modifier  : 0
      }
    , calcMod ( ability )
      { 
      return this[ability].modifier = 0 | ((this[ability].base - 10) / 2);       
  } } } 

console.log( characterSheet.abilityScore.calcMod( 'strength' ) )

console.log( characterSheet.abilityScore.strength.modifier )

OR:

const characterSheet =
  { abilityScore: 
    { strength     : { base :  2, modifier : 0 }
    , dexterity    : { base :  8, modifier : 0 }
    , intelligence : { base : 16, modifier : 0 }
  } };
  
function calcMod( val ) 
  {
  return val.modifier = 0 | ((val.base - 10) / 2);
  }

console.log( calcMod( characterSheet.abilityScore.strength ))
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
1

You should use

function buildScore(base) {
  return {
    base,
    modifier: Math.floor((base - 10) / 2),
  };
}
const characterSheet = {
  abilityScore: {
    strength: buildScore(2),
    dexterity: buildScore(8),
    intelligence: buildScore(16),
  },
};

console.log(characterSheet.abilityScore.strength);

No methods, no problems. You could also achieve something like this with getters, but it would be much more complicated.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

You can use a getter for modifier that calls calcMod(). This calls a function to return the value of the property, so it can calculate it based on the current value of this.base.

function calcMod(val) {
  return Math.floor((val - 10) / 2);
}

const characterSheet = {
  abilityScore: {
    strength: {
      base: 2,
      get modifier() {
        return calcMod(this.base);
      }
    },
    dexterity: {
      base: 8,
      get modifier() {
        return calcMod(this.base);
      }
    },
    intelligence: {
      base: 16,
      get modifier() {
        return calcMod(this.base);
      }
    },
  },
}

console.log(characterSheet.abilityScore.dexterity.modifier);
characterSheet.abilityScore.dexterity.base = 10;
console.log(characterSheet.abilityScore.dexterity.modifier);
Barmar
  • 741,623
  • 53
  • 500
  • 612