Skip to content

Instantly share code, notes, and snippets.

@colevandersWands
Last active April 23, 2018 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colevandersWands/190283ff2a4d0302aca88086022ab71c to your computer and use it in GitHub Desktop.
Save colevandersWands/190283ff2a4d0302aca88086022ab71c to your computer and use it in GitHub Desktop.
fp vs. oop water bottle
const bottle_size = 1000;
let bottle_fullness = 600;
function read_water_level(bottle_fullness) {
console.log(bottle_fullness)
};
function drink_water(bottle_fullness, thrist_score) { // one unit of water quenches 1 thrist point
let new_fullness = 0;
let remaining_thirst = 0;
// let _new_fullness = 0; // for ES5 notation example
// let _remaining_thirst = 0;
// fill this in
return { new_fullness, remaining_thirst }; // es6 syntax: https://codeburst.io/es2015-quickie-enhanced-object-literals-ae0d41b60465
// {new_fullness: _new_fullness, remaining_thirst: _remaining_thirst}; // ES5 notation example
// PythonTutor - https://goo.gl/LzQ3P7
}
function fill_bottle(bottle_fullness, bottle_size, water_purchased) {
let new_fullness = 0;
let remaining_water = 0;
// fill this in
return {new_fullness, remaining_water};
}
// ----- day in the life -----
bottle_fullness = fill_bottle(bottle_fullness, bottle_size, 700).new_fullness;
// let fill_bottle_returned = fill_bottle(bottle_fullness, bottle_size, 700);
// bottle_fullness = fill_bottle_returned.new_fullness;
let thirst = 400;
let drink_water_returned = drink_water(bottle_fullness, 600);
thirst = drink_water_returned.remaining_thirst; // 1000
bottle_fullness = drink_water_returned.new_fullness; // 400
let water_bottle = {
capacity: 1000,
fullness: 600,
read_water_level: function() {
console.log(this.fullness);
},
drunk_from: function(thirst_score) {
let remaining_thirst = 0;
// update this.fullness
return remaining_thirst;
},
}
// going off the deep end:
// if you wanted to include thirst in this program:
// let person = {
// thirst: 300,
// drinks_from_bottle: function(a_water_bottle) {
// a_water_bottle.drunk_from(300)
// this.thirst -= 300 (or whatever was left in the bottle)
// }
// }
// water_bottle.drink_water(person.get_thirst());
// let drinking_app = {
// person: person,
// water_bottle: water_bottle,
// drink_from: function(a_number) {
// // subtract this from bottle, add it to person
// }
// }
build two watter bottle applications:
state: water in bottle
user stories:
- drink water
- add water
- see how much is left
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment