Created
February 7, 2023 17:04
-
-
Save telagraphic/cbf5cd14a4e08045e407c5f5245567ee to your computer and use it in GitHub Desktop.
treasure-chest-options
This file contains hidden or 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 - User Options</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 - User Options</h1> | |
<p>All of the magic here happens in the console.</p> | |
<script> | |
let TreasureChest = (function() { | |
/** | |
* Create the constructor object | |
*/ | |
function Constructor(options = {}) { | |
let { | |
gold, | |
silver, | |
bronze | |
} = Object.assign({ | |
gold: 0, | |
silver: 0, | |
bronze: 0 | |
}, options); | |
this.bronze = bronze; | |
this.silver = silver; | |
this.gold = gold; | |
}; | |
/** | |
* 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; | |
} | |
/** | |
* Add bronze to the treasure chest | |
* @param {Number} n The amount to add | |
*/ | |
Constructor.prototype.addBronze = function(n) { | |
this.bronze += n; | |
return this; | |
}; | |
/** | |
* Add silver to the treasure chest | |
* @param {Number} n The amount to add | |
*/ | |
Constructor.prototype.addSilver = function(n) { | |
this.silver += n; | |
return this; | |
}; | |
/** | |
* Add gold to the treasure chest | |
* @param {Number} n The amount to add | |
*/ | |
Constructor.prototype.addGold = function(n) { | |
this.gold += n; | |
return this; | |
}; | |
/** | |
* Get the total amount of loot as a string | |
* @return {String} The total amount of loot | |
*/ | |
Constructor.prototype.getLoot = function(person) { | |
return `${person} have ${this.gold} gold, ${this.silver} silver, and ${this.bronze} bronze.`; | |
}; | |
/** | |
* Generate random treasure | |
* @return {Object} The amount and type of loot found | |
*/ | |
Constructor.getRandomLoot = function() { | |
// Amount and type | |
let amount = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]; | |
let type = ['bronze', 'silver', 'gold']; | |
// Randomize the amounts | |
shuffle(amount); | |
shuffle(type); | |
// Return the random loot | |
return { | |
amount: amount[0], | |
type: type[0] | |
}; | |
}; | |
return Constructor; | |
})(); | |
let captain = new TreasureChest({ | |
gold: 20, | |
silver: 40 | |
}); | |
let captainLoot = captain.getLoot("captain"); | |
console.log(captainLoot); | |
let captainGold = captain.gold; | |
console.log(captainGold) | |
let firstOfficer = new TreasureChest(); | |
let firstOfficerLoot = firstOfficer.getLoot("first offiver"); | |
console.log(firstOfficerLoot); | |
let firstOfficerBronze = firstOfficer.bronze; | |
console.log(firstOfficerBronze); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment