Last active
October 26, 2025 20:28
-
-
Save binki/22558c8657faf6e8210b2ab267f44630 to your computer and use it in GitHub Desktop.
Binki Coffee Mug While Working JavaScript implementation
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
| const coffee = { | |
| remainingCoffee: 1, | |
| drink: function () { | |
| console.log(`drank coffee, ${--this.remainingCoffee} remaining`); | |
| }, | |
| refill: function () { | |
| console.log(`refilling coffee mug with 2 sips`); | |
| this.remainingCoffee += 2; | |
| coffeePot.remainingCoffee -= 2; | |
| }, | |
| toString: function () { | |
| return this.remainingCoffee === 0 ? `empty` : `${this.remainingCoffee} coffee sips in coffee mug`; | |
| }, | |
| }; | |
| let working = true; | |
| const work = { | |
| workRemaining: 4, | |
| execute: function () { | |
| console.log(`executing work, ${--this.workRemaining} remaining`); | |
| working = !!this.workRemaining; | |
| }, | |
| }; | |
| const coffeePot = { | |
| remainingCoffee: 0, | |
| brew: function () { | |
| this.remainingCoffee += 4; | |
| console.log(`brewing 4 sips of coffee, pot now has ${this.remainingCoffee} sips left`); | |
| }, | |
| toString: function () { | |
| return this.remainingCoffee === 0 ? `empty` : `${this.remainingCoffee} coffee sips in coffee pot`; | |
| }, | |
| }; | |
| while(working) | |
| { | |
| coffee.drink(); | |
| work.execute(); | |
| if(coffee == "empty") | |
| { | |
| if(coffeePot == "empty") | |
| coffeePot.brew(); | |
| coffee.refill(); | |
| } | |
| } |
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
| ohnob@binki-legion-pc MSYS /c/Users/ohnob/repos/binki-coffee-mug-implementation | |
| $ /mingw64/bin/node binki-coffee-mug-implementation.js | |
| drank coffee, 0 remaining | |
| executing work, 3 remaining | |
| brewing 4 sips of coffee, pot now has 4 sips left | |
| refilling coffee mug with 2 sips | |
| drank coffee, 1 remaining | |
| executing work, 2 remaining | |
| drank coffee, 0 remaining | |
| executing work, 1 remaining | |
| refilling coffee mug with 2 sips | |
| drank coffee, 1 remaining | |
| executing work, 0 remaining |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment