Add a bunch of projects

This commit is contained in:
Andrea Fazzi 2024-08-19 08:19:15 +02:00
parent 7ff6c472b2
commit e595b5d81e
7 changed files with 229 additions and 0 deletions

View file

@ -13,6 +13,8 @@ func main() {
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"))))
mux.Handle("GET /treasure-chest-js-class/", http.StripPrefix("/treasure-chest-js-class", http.FileServer(http.Dir("treasure-chest-js-class"))))
mux.Handle("GET /treasure-chest-private/", http.StripPrefix("/treasure-chest-private", http.FileServer(http.Dir("treasure-chest-private"))))
log.Println("Start the web server...")
err := http.ListenAndServe(":8080", mux)

View 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/

View file

@ -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 - JS Class</title>
</head>
<body>
<h1>Treasure Chest - JS Classs</h1>
<p>All of the magic here happens in the console.</p>
<script src="script.js"></script>
</body>
</html>

View file

@ -0,0 +1,83 @@
class TreasureChest {
constructor (initialLoot = {}) {
// Merge options into defaults
let {bronze, silver, gold} = 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}
});
}
addGold (qty) {
this.gold += qty;
return this;
}
addSilver (qty) {
this.silver += qty;
return this;
}
addBronze (qty) {
this.bronze += qty;
return this;
}
getLoot () {
return `Gold: ${this.gold}, Silver: ${this.silver}, Bronze: ${this.bronze}`;
}
/**
* Randomly shuffle an array
* https://stackoverflow.com/a/2450976/1293256
* @param {Array} array The array to shuffle
* @return {Array} The shuffled array
*/
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;
}
static getRandomLoot () {
let amount = [];
for (let qty = 0; qty < 50; qty++) {
amount.push(qty+1);
}
return {
gold: this.shuffle(amount)[0],
silver: this.shuffle(amount)[0],
bronze: this.shuffle(amount)[0],
}
}
};

View 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/

View file

@ -0,0 +1,14 @@
<!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 - Private Class Features</title>
</head>
<body>
<h1>Treasure Chest - Private Class Features</h1>
<p>All of the magic here happens in the console.</p>
<script src="script.js"></script>
</body>
</html>

View file

@ -0,0 +1,95 @@
class TreasureChest {
#gold;
#silver;
#bronze;
constructor (initialLoot = {}) {
// Merge options into defaults
let {bronze, silver, gold} = Object.assign({
bronze: 0,
silver: 0,
gold: 0,
}, initialLoot);
this.#bronze = bronze;
this.#silver = silver;
this.#gold = gold;
}
addGold (qty) {
this.#gold += qty;
return this;
}
addSilver (qty) {
this.#silver += qty;
return this;
}
addBronze (qty) {
this.#bronze += qty;
return this;
}
getBronze () {
return this.#bronze;
}
getSilver () {
return this.#silver;
}
getGold () {
return this.#gold;
}
getLoot () {
return `Gold: ${this.#gold}, Silver: ${this.#silver}, Bronze: ${this.#bronze}`;
}
/**
* Randomly shuffle an array
* https://stackoverflow.com/a/2450976/1293256
* @param {Array} array The array to shuffle
* @return {Array} The shuffled array
*/
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;
}
static getRandomLoot () {
let amount = [];
for (let qty = 0; qty < 50; qty++) {
amount.push(qty+1);
}
return {
gold: this.#shuffle(amount)[0],
silver: this.#shuffle(amount)[0],
bronze: this.#shuffle(amount)[0],
}
}
};