Add Treasure Chest with Chaining and Static Methods
This commit is contained in:
parent
3dcdcbbac5
commit
89def88673
4 changed files with 97 additions and 0 deletions
|
@ -11,6 +11,7 @@ func main() {
|
||||||
|
|
||||||
mux.Handle("GET /dice-library/", http.StripPrefix("/dice-library", http.FileServer(http.Dir("dice-library"))))
|
mux.Handle("GET /dice-library/", http.StripPrefix("/dice-library", http.FileServer(http.Dir("dice-library"))))
|
||||||
mux.Handle("GET /treasure-chest/", http.StripPrefix("/treasure-chest", http.FileServer(http.Dir("treasure-chest"))))
|
mux.Handle("GET /treasure-chest/", http.StripPrefix("/treasure-chest", http.FileServer(http.Dir("treasure-chest"))))
|
||||||
|
mux.Handle("GET /treasure-chest-chaining-static/", http.StripPrefix("/treasure-chest-chaining-static", http.FileServer(http.Dir("treasure-chest-chaining-static"))))
|
||||||
|
|
||||||
log.Println("Start the web server...")
|
log.Println("Start the web server...")
|
||||||
err := http.ListenAndServe(":8080", mux)
|
err := http.ListenAndServe(":8080", mux)
|
||||||
|
|
10
structure-and-scale/treasure-chest-chaining-static/README.md
Normal file
10
structure-and-scale/treasure-chest-chaining-static/README.md
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
# Treasure Chest Library - Chaining and Static Methods
|
||||||
|
|
||||||
|
A library that pirates can use to track the amount of loot they find
|
||||||
|
on their travels.
|
||||||
|
|
||||||
|
The library willsupport chained methods and add static methods.
|
||||||
|
|
||||||
|
# References
|
||||||
|
|
||||||
|
* https://leanwebclub.com/learn/structure-and-scale/project-treasure-chest-library-chaining-and-static-methods/
|
|
@ -0,0 +1,15 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en" data-bs-theme="dark">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
|
||||||
|
<title>Treasure Chest - Chaining and Static Methods</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Treasure Chest - Chaining and Static Methods</h1>
|
||||||
|
<p>All of the magic here happens in the console.</p>
|
||||||
|
|
||||||
|
<script src="script.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
71
structure-and-scale/treasure-chest-chaining-static/script.js
Normal file
71
structure-and-scale/treasure-chest-chaining-static/script.js
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
let TreasureChest = (function () {
|
||||||
|
function Constructor () {
|
||||||
|
this.loot = { gold: 0, silver: 0, bronze: 0 };
|
||||||
|
}
|
||||||
|
|
||||||
|
Constructor.prototype.addGold = function (qty) {
|
||||||
|
this.loot.gold += qty;
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Constructor.prototype.addSilver = function (qty) {
|
||||||
|
this.loot.silver += qty;
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Constructor.prototype.addBronze = function (qty) {
|
||||||
|
this.loot.bronze += qty;
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Randomly shuffle an array
|
||||||
|
* https://stackoverflow.com/a/2450976/1293256
|
||||||
|
* @param {Array} array The array to shuffle
|
||||||
|
* @return {Array} The shuffled array
|
||||||
|
*/
|
||||||
|
function 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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Constructor.getRandomLoot = function () {
|
||||||
|
let amount = [];
|
||||||
|
|
||||||
|
for (let qty = 0; qty <= 50; qty++) {
|
||||||
|
amount.push(qty+1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
gold: shuffle(amount)[0],
|
||||||
|
silver: shuffle(amount)[0],
|
||||||
|
bronze: shuffle(amount)[0],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Constructor.prototype.getLoot = function () {
|
||||||
|
return `Gold: ${this.loot.gold}, Silver: ${this.loot.silver}, Bronze: ${this.loot.bronze}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Constructor;
|
||||||
|
})();
|
||||||
|
|
Loading…
Reference in a new issue