Skip to content

Instantly share code, notes, and snippets.

@telagraphic
Created February 6, 2023 14:58
Show Gist options
  • Save telagraphic/004c6e02ec89d3766bc67e5da21941f1 to your computer and use it in GitHub Desktop.
Save telagraphic/004c6e02ec89d3766bc67e5da21941f1 to your computer and use it in GitHub Desktop.
treasure-chest-chaining
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Treasure Chest Chaining</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</h1>
<p>All of the magic here happens in the console.</p>
<script>
let TreasureChest = (function() {
function Constructor() {
this.bronze = 0;
this.silver = 0;
this.gold = 0;
}
Constructor.prototype.addBronze = function(bronze) {
this.bronze = this.bronze + bronze;
return this;
}
Constructor.prototype.addSilver = function(silver) {
this.silver = this.silver + silver;
return this;
}
Constructor.prototype.addGold = function(gold) {
this.gold = this.gold + gold;
return this;
}
Constructor.prototype.getLoot = function() {
console.log(`
Bronze stock is ${this.bronze}
Silver stock is ${this.silver}
Gold stock is ${this.gold}
`);
}
Constructor.getRandomLoot = function() {
return Math.floor(Math.random() * 50);
}
return Constructor;
})();
let chest = new TreasureChest();
chest.addBronze(10).addSilver(20).addGold(30);
chest.getLoot();
let randomAmount = TreasureChest.getRandomLoot();
console.log(randomAmount);
chest.addGold(randomAmount);
chest.getLoot();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment