Last active
April 23, 2018 13:54
-
-
Save 1n3ffbl3/6fbcf77e6c9db523b9dfb11b16f3f40a to your computer and use it in GitHub Desktop.
Almost perfect water bottle solution + specs
This file contains 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
let bottle_state = 0; | |
let extra_water= 0; | |
function read_water_level(bottle_state){ | |
console.log(bottle_state) | |
}; | |
function drink_water(bottle_fullness, thirst_score, bottle_size) { | |
let new_bottle_state = 0; | |
let remaining_thirst = 0; | |
if (bottle_fullness > 0 && thirst_score > 0 && bottle_fullness > thirst_score) { | |
new_bottle_state = bottle_fullness - thirst_score; | |
remaining_thirst = 0; | |
} else if (bottle_fullness > 0 && thirst_score > 0 && bottle_fullness < thirst_score) { | |
new_bottle_state = bottle_size; | |
remaining_thirst = bottle_size - thirst_score; | |
} else if (bottle_fullness == thirst_score) { | |
new_bottle_state = 0; | |
remaining_thirst = 0; | |
} else { | |
alert("Error"); | |
} | |
return { new_bottle_state, remaining_thirst }; // es6 syntax | |
}; | |
console.log(drink_water(200, 100, 600)); | |
// = return {new_bottle_state: new_bottle_state, remaining_thirst: remaining_thirst} | |
}; | |
function fill_bottle_with_water (bottle_fullness, bottle_size, water_purchased){ | |
let new_bottle_state_to_fill = 0; | |
let water_left_over= 0; | |
if (bottle_fullness > 0 && water_purchased > 0 && bottle_fullness< water_purchased || | |
bottle_fullness > 0 && water_purchased > 0 && bottle_fullness> water_purchased && water_purchased< bottle_size){ | |
new_bottle_state_to_fill = bottle_fullness + water_purchased; | |
water_left_over = 0; | |
}else if(bottle_fullness == bottle_size && water_purchased >= 0){ | |
new_bottle_state_to_fill = bottle_size; | |
water_left_over= water_purchased; | |
}else{ | |
alert("Error"); | |
} | |
return {new_bottle_state_to_fill, water_left_over}; | |
}; |
This file contains 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
GLOBAL SCOPE: | |
VARIABLE1: bottle_state | |
Initialized: undefined | |
Purpose: It shows water level in the bottle | |
VARIABLE2: extra_water | |
Initialized: undefined | |
Purpose: It shows extra water | |
read_water_level: function | |
ARGS:1 | |
bottle_state: DOM object | |
PURPOSE: This function reads water level in the bottle | |
BEHAVIOR: It calls bottle state of water | |
drink_water: function | |
ARGS: 2 | |
bottle_fullness: first argument | |
PURPOSE: It shows the water level in the bottle at the initial stage | |
thirst_score: second argument | |
PURPOSE: It shows the thirst level | |
bottle_size: third argument | |
PURPOSE: It tells us the size of the bottle so we know the maximum quantity of water which can be filled into the bottle | |
RETURN: An object | |
VARIABLE1: new_bottle_state | |
Initialized: undefined | |
VARIABLE2: remaining_thirst | |
Initialized: undefined | |
BEHAVIOR: This function calculates the new bottle state after drinking and our remaining thirst score. Then it returns an object with new variables mentioned before. | |
fill_bottle_with_water : function | |
ARGS: 3 | |
RETURN: An array | |
bottle_fullness: first argument | |
PURPOSE: It shows the water level in the bottle at the initial stage | |
bottle_size: second argument | |
PURPOSE: It tells us the size of the bottle | |
water_purchased: third argument | |
PURPOSE: It tells us the quantity of the purchased water | |
VARIABLE1: new_bottle_state | |
Initialized: undefined | |
VARIABLE2: water_left_over | |
Initialized: undefined | |
BEHAVIOR: This function calculates the new bottle state after being filled with purchased water and the amount of water remaining after filling. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
corrections aside, very good. you're now using behavior and purpose correctly