| 3 | 3 | // convenience to get you started writing code faster. |
| 4 | 4 | // |
| 5 | 5 | |
| 6 | | export const abilityModifier = () => { |
| 7 | | throw new Error('Remove this line and implement the function'); |
| 6 | export const abilityModifier = (score) => { |
| 7 | // Edge Case: Score less than 3 (minimum possible ability score) |
| 8 | if (score < 3) { |
| 9 | throw new Error('Ability scores must be at least 3'); |
| 10 | } |
| 11 | |
| 12 | // Edge Case: Score greater than 18 (maximum possible ability score) |
| 13 | if (score > 18) { |
| 14 | throw new Error('Ability scores can be at most 18'); |
| 15 | } |
| 16 | |
| 17 | return Math.floor((score - 10) / 2); |
| 8 | 18 | }; |
| 9 | 19 | |
| 10 | 20 | export class Character { |
| 21 | constructor() { |
| 22 | this._strength = Character.rollAbility(); |
| 23 | this._dexterity = Character.rollAbility(); |
| 24 | this._constitution = Character.rollAbility(); |
| 25 | this._intelligence = Character.rollAbility(); |
| 26 | this._wisdom = Character.rollAbility(); |
| 27 | this._charisma = Character.rollAbility(); |
| 28 | } |
| 29 | |
| 11 | 30 | static rollAbility() { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 31 | // Roll 4 dice (random numbers between 1 and 6) |
| 32 | const rolls = Array.from({ length: 4 }, () => Math.floor(Math.random() * 6) + 1); |
| 33 | |
| 34 | // Sort the rolls and drop the smallest one |
| 35 | rolls.sort((a, b) => a - b); |
| 36 | rolls.shift(); // Remove the smallest roll |
| 37 | |
| 38 | // Sum the remaining three rolls |
| 39 | return rolls.reduce((sum, roll) => sum + roll, 0); |
| 13 | 40 | } |
| 14 | 41 | |
| 15 | 42 | get strength() { |
| 16 | | throw new Error('Remove this line and implement the function'); |
| 43 | return this._strength; |
| 17 | 44 | } |
| 18 | 45 | |
| 19 | 46 | get dexterity() { |
| 20 | | throw new Error('Remove this line and implement the function'); |
| 47 | return this._dexterity; |
| 21 | 48 | } |
| 22 | 49 | |
| 23 | 50 | get constitution() { |
| 24 | | throw new Error('Remove this line and implement the function'); |
| 51 | return this._constitution; |
| 25 | 52 | } |
| 26 | 53 | |
| 27 | 54 | get intelligence() { |
| 28 | | throw new Error('Remove this line and implement the function'); |
| 55 | return this._intelligence; |
| 29 | 56 | } |
| 30 | 57 | |
| 31 | 58 | get wisdom() { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 59 | return this._wisdom; |
| 33 | 60 | } |
| 34 | 61 | |
| 35 | 62 | get charisma() { |
| 36 | | throw new Error('Remove this line and implement the function'); |
| 63 | return this._charisma; |
| 37 | 64 | } |
| 38 | 65 | |
| 39 | 66 | get hitpoints() { |
| 40 | | throw new Error('Remove this line and implement the function'); |
| 67 | // 10 + constitution modifier |
| 68 | return 10 + abilityModifier(this._constitution); |
| 41 | 69 | } |
| 42 | 70 | } |
| 71 | |
| 72 | // Handled Edge Cases: |
| 73 | // - abilityModifier: Score less than 3, Score greater than 18 |