Created
February 21, 2023 22:34
-
-
Save angusgrant/8726a22636e49b22fdd17b01a4feb616 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> | |
let TreasureChest = (function () { | |
/** | |
* Create the Constuctor object | |
* @param {Obj} obj the starting total for gold , silver, bronze | |
*/ | |
function Constructor (gold = 0, silver = 0, bronze = 0) { | |
this.gold = gold; | |
this.silver = silver; | |
this.bronze = bronze; | |
} | |
Constructor.getRandomLoot = function() { | |
this.groat = shuffle([...Array(50).keys()])[0] + 1; | |
this.silver = shuffle([...Array(50).keys()])[0] + 1; | |
this.bronze = shuffle([...Array(50).keys()])[0] + 1; | |
return `Total Random Loot is: Bronze = ${this.groat}, Silver = ${this.silver}, Gold = ${this.gold}`; | |
} | |
this.shuffle = function(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 | |
*/ | |
Constructor.prototype.addGold = function (num) { | |
this.gold += num; | |
return this; | |
}; | |
/** | |
* Create the Constuctor method addSilver | |
* @param {Number} num the additional Silver to add | |
*/ | |
Constructor.prototype.addSilver = function (num) { | |
this.silver += num; | |
return this; | |
}; | |
/** | |
* Create the Constuctor method addBronze | |
* @param {Number} num the additional Bronze to add | |
*/ | |
Constructor.prototype.addBronze = function (num) { | |
this.bronze += num; | |
return this; | |
}; | |
/** | |
* Create the Constuctor method getLoot | |
*/ | |
Constructor.prototype.getLoot = function () { | |
return `Total Swag is: Bronze = ${this.bronze}, Silver = ${this.silver}, Gold = ${this.gold}` | |
} | |
return Constructor; | |
})(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment