Skip to content

Instantly share code, notes, and snippets.

@angusgrant
Created February 23, 2023 09:21
Show Gist options
  • Save angusgrant/e51dbbf66c0dee0bf55a9bcc33679605 to your computer and use it in GitHub Desktop.
Save angusgrant/e51dbbf66c0dee0bf55a9bcc33679605 to your computer and use it in GitHub Desktop.
Go Make Things. Structure and Scale. Week 2 Task 2.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Treasure Chest</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
body {
margin: 0 auto;
max-width: 40em;
width: 88%;
}
</style>
</head>
<body>
<h1>Treasure Chest</h1>
<p>All of the magic here happens in the console.</p>
<script>
class TreasureChest {
/**
* Create the Constuctor object
* @param {Obj} obj the starting total for gold , silver, bronze
*/
constructor (options = {}) {
let {gold, silver, bronze} = Object.assign({
gold: 0,
silver: 0,
bronze: 0
}, options);
Object.defineProperties(this, {
gold: {
value: gold,
writable: true
},
silver: {
value: silver,
writable: true
},
bronze: {value: bronze,
writable: true
}
})
}
static getRandomLoot() {
this.gold = this.#shuffle([...Array(50).keys()])[0] + 1;
this.silver = this.#shuffle([...Array(50).keys()])[0] + 1;
this.groat = this.#shuffle([...Array(50).keys()])[0] + 1;
return `Total Random Loot is: Bronze = ${this.groat}, Silver = ${this.silver}, Gold = ${this.gold}`;
}
static #shuffle(array) {
let currentIndex = array.length;
let temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
/**
* Create the Constuctor method addGold
* @param {Number} num the additional Gold to add
*/
addGold = function (num) {
this.gold += num;
return this;
};
/**
* Create the Constuctor method addSilver
* @param {Number} num the additional Silver to add
*/
addSilver = function (num) {
this.silver += num;
return this;
};
/**
* Create the Constuctor method addBronze
* @param {Number} num the additional Bronze to add
*/
addBronze = function (num) {
this.bronze += num;
return this;
};
/**
* Create the Constuctor method getLoot
*/
getLoot = function () {
return `Total Swag is: Bronze = ${this.bronze}, Silver = ${this.silver}, Gold = ${this.gold}`
}
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment