Menu
Coddy logo textTech

Game Character Builder

Part of the Object Oriented Programming section of Coddy's JavaScript journey — lesson 53 of 56.

challenge icon

Challenge

This is a simple game character system where characters can equip weapons and engage in combat.

Your task is to add these two methods to the Character class:

Method 1: takeDamage(amount)

  • This method reduces the character's health by the amount
  • Logs: ${this.name} takes ${amount} damage

Method 2: equipWeapon(weapon)

  • This method gives the character a weapon (composition: "has-a" relationship)
  • Logs: ${this.name} equipped ${weapon.name}

Try it yourself

import { Character } from './character.js';
import { Weapon } from './weapon.js';

const hero = new Character('Knight');
const sword = new Weapon('Sword', 10);

hero.equipWeapon(sword); // Should log: "Knight equipped Sword"
hero.takeDamage(20);     // Should log: "Knight takes 20 damage"
console.log(hero.health); // Should show: 80

All lessons in Object Oriented Programming