scrimba
Create an RPG game - Rob Sutcliffe
Create a second character from the same object
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Create a second character from the same object
by
AboutCommentsNotes
Create a second character from the same object
by
Expand for more info
index.js
run
preview
console
import characterData from "./data";
import { getDiceRollArray, sumArray } from "./utils";

function Character(data) {

Object.assign(this, data);

this.currentDiceRole = [];

this.rollDice = function() {
this.currentDiceRole = getDiceRollArray(this.diceCount);
};

this.getDiceElementArray = function() {
return this.currentDiceRole.map(function(num) {
return `<div class="dice">${num}</div>`
});
};

this.getTemplate = function() {
const { name, emoji, health } = this;
const diceElementArray = this.getDiceElementArray();

return `<div class="character">
<div class="name">${name}</div>
<div class="emoji">️${emoji}</div>
<div class="health">health: <b>${health}</b></div>
<div class="dice-container">
${diceElementArray}
</div>
</div>`;
};
};

// add the monster character
const hero = new Character(characterData.hero);
document.getElementById(hero.elementId).innerHTML = hero.getTemplate();

Console
/index.html
-1:27