diff --git a/structure-and-scale/main.go b/structure-and-scale/main.go index 93025f4..b008d91 100644 --- a/structure-and-scale/main.go +++ b/structure-and-scale/main.go @@ -12,6 +12,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")))) + mux.Handle("GET /treasure-chest-user-options/", http.StripPrefix("/treasure-chest-user-options", http.FileServer(http.Dir("treasure-chest-user-options")))) log.Println("Start the web server...") err := http.ListenAndServe(":8080", mux) diff --git a/structure-and-scale/treasure-chest-user-options/README.md b/structure-and-scale/treasure-chest-user-options/README.md new file mode 100644 index 0000000..0b52977 --- /dev/null +++ b/structure-and-scale/treasure-chest-user-options/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-user-options/index.html b/structure-and-scale/treasure-chest-user-options/index.html new file mode 100644 index 0000000..eab6aa2 --- /dev/null +++ b/structure-and-scale/treasure-chest-user-options/index.html @@ -0,0 +1,15 @@ + + + + + + + Treasure Chest - User Options + + +

Treasure Chest - User Options

+

All of the magic here happens in the console.

+ + + + diff --git a/structure-and-scale/treasure-chest-user-options/script.js b/structure-and-scale/treasure-chest-user-options/script.js new file mode 100644 index 0000000..cf3f8e0 --- /dev/null +++ b/structure-and-scale/treasure-chest-user-options/script.js @@ -0,0 +1,84 @@ +let TreasureChest = (function () { + + function Constructor (initialLoot = {}) { + + // Merge options into defaults + let {bronze, silver, gold, loot} = Object.assign({ + bronze: 0, + silver: 0, + gold: 0, + }, initialLoot); + + Object.defineProperties(this, { + gold: {value: gold, writable: true}, + silver: {value: silver, writable: true}, + bronze: {value: bronze, writable: true} + }); + } + + Constructor.prototype.addGold = function (qty) { + this.gold += qty; + + return this; + } + + Constructor.prototype.addSilver = function (qty) { + this.silver += qty; + + return this; + } + + Constructor.prototype.addBronze = function (qty) { + this.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.gold}, Silver: ${this.silver}, Bronze: ${this.bronze}`; + } + + return Constructor; +})(); +