From f8d3042557b4611bb774fd3f3abc7d077a2a186a Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Tue, 16 Sep 2025 00:50:21 +0000 Subject: [PATCH] [Sync Iteration] typescript/dnd-character/2 --- .../dnd-character/2/dnd-character.ts | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 solutions/typescript/dnd-character/2/dnd-character.ts diff --git a/solutions/typescript/dnd-character/2/dnd-character.ts b/solutions/typescript/dnd-character/2/dnd-character.ts new file mode 100644 index 0000000..b2ba14c --- /dev/null +++ b/solutions/typescript/dnd-character/2/dnd-character.ts @@ -0,0 +1,44 @@ +function rollDice():number { + return Math.floor(Math.random() * 6) + 1; +} +function getAbility():number { + const ability = Array.from({ length: 4 }, rollDice); + return ability.sort((a,b)=> b - a).slice(0,1).reduce((value, accumulator) => accumulator + value, 0); +} + +export class DnDCharacter { + strength: number; + dexterity: number; + constitution: number; + intelligence: number; + wisdom: number; + charisma: number; + hitpoints: number; + + constructor() { + this.strength = getAbility(); + this.dexterity = getAbility(); + this.constitution = getAbility(); + this.intelligence = getAbility(); + this.wisdom = getAbility(); + this.charisma = getAbility(); + + this.hitpoints = 10 + DnDCharacter.getModifierFor(this.constitution); + } + public static generateAbilityScore(): number { + const character = { + strength: getAbility(), + dexterity: getAbility(), + constitution: getAbility(), + intelligence: getAbility(), + wisdom: getAbility(), + charisma: getAbility(), + }; + const constitutionModifier = this.getModifierFor(character.constitution); + return 10 + constitutionModifier + } + + public static getModifierFor(abilityValue: number): number { + return Math.floor((abilityValue - 10) / 2); + } +}