From 89def886735406d442802d1a8e14468fedec174e Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Fri, 16 Aug 2024 19:14:39 +0200 Subject: [PATCH] Add Treasure Chest with Chaining and Static Methods --- structure-and-scale/main.go | 1 + .../treasure-chest-chaining-static/README.md | 10 +++ .../treasure-chest-chaining-static/index.html | 15 ++++ .../treasure-chest-chaining-static/script.js | 71 +++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 structure-and-scale/treasure-chest-chaining-static/README.md create mode 100644 structure-and-scale/treasure-chest-chaining-static/index.html create mode 100644 structure-and-scale/treasure-chest-chaining-static/script.js diff --git a/structure-and-scale/main.go b/structure-and-scale/main.go index cc619ba..93025f4 100644 --- a/structure-and-scale/main.go +++ b/structure-and-scale/main.go @@ -11,6 +11,7 @@ func main() { 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-chaining-static/", http.StripPrefix("/treasure-chest-chaining-static", http.FileServer(http.Dir("treasure-chest-chaining-static")))) log.Println("Start the web server...") err := http.ListenAndServe(":8080", mux) diff --git a/structure-and-scale/treasure-chest-chaining-static/README.md b/structure-and-scale/treasure-chest-chaining-static/README.md new file mode 100644 index 0000000..0b52977 --- /dev/null +++ b/structure-and-scale/treasure-chest-chaining-static/README.md @@ -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/ diff --git a/structure-and-scale/treasure-chest-chaining-static/index.html b/structure-and-scale/treasure-chest-chaining-static/index.html new file mode 100644 index 0000000..2e29a55 --- /dev/null +++ b/structure-and-scale/treasure-chest-chaining-static/index.html @@ -0,0 +1,15 @@ + + + + + + + Treasure Chest - Chaining and Static Methods + + +

Treasure Chest - Chaining and Static Methods

+

All of the magic here happens in the console.

+ + + + diff --git a/structure-and-scale/treasure-chest-chaining-static/script.js b/structure-and-scale/treasure-chest-chaining-static/script.js new file mode 100644 index 0000000..bdeadf1 --- /dev/null +++ b/structure-and-scale/treasure-chest-chaining-static/script.js @@ -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; +})(); +