Skip to content

Instantly share code, notes, and snippets.

@leodutra
Last active September 8, 2022 04:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leodutra/7593753239918b6893fc4e97d0350f95 to your computer and use it in GitHub Desktop.
Save leodutra/7593753239918b6893fc4e97d0350f95 to your computer and use it in GitHub Desktop.
How much water should you drink per day? ( JavaScript )
// WARNING!
// Water Intoxication.
// This is rare but it can happen.
// What happens is that when too much water enters the cells, the tissues swell.
// This causes an electrolyte and salt imbalance which can cause irregular heart
// beat and allow fluid to enter the lungs.
// The pressure due to swelling will also put pressure on the brain and nerves,
// which can also cause problems.
// Swelling in the brain can cause coma, seizures, and even death.
// REFS:
// https://ec.europa.eu/jrc/en/health-knowledge-gateway/promotion-prevention/nutrition/water
// https://www.bodybuilding.com/fun/weik46.htm
// https://iwaterpurification.com/how-much-water-should-i-drink/
// https://www.slenderkitchen.com/article/how-to-calculate-how-much-water-you-should-drink-a-day
/*
The Mayo Clinic tells us to drink when we are thirsty, but that some people may need those 8 glasses.
There are reasons some people may need more water than others.
Harvard Medical School tells us that the 8 cups a day as a rule is essentially just nonsense.
They do say, however that we need to be drinking water throughout our day in order to be sure
to stay properly hydrated. We need to be staying hydrated daily,
not just in special circumstances like in the blazing heat, or during high activity levels.
They caution older adults to always remember to drink more water because the sense for being
thirsty can slowly reduce as we get older.
Many older adults are also taking medications that can cause fluid loss in addition to
normal urine and sweat loss.
Diuretics for blood pressure, swelling or any other reason will make an older adult more
prone to needing more fluids and therefore not drinking enough water.
*/
const WEIGHT = 85
const AGE = 30
const POUNDS_PER_KG = 2.205
const OUNCE_PER_LITER = 33.814
const kgToPounds = kg => kg * POUNDS_PER_KG
const ounceToLiter = ounces => ounces / OUNCE_PER_LITER
const formatOunces = ounces => ounces.toFixed(2) + ' ozs'
const formatLiters = liters => liters.toFixed(2) + ' L'
function glassesPerDay(age, weight, useKg = true) {
if (useKg) {
weight = kgToPounds(weight)
}
const kg = weight / POUNDS_PER_KG
let ratioPerAge = 0
if (age < 30) {
ratioPerAge = 40
} else if (age <= 30 || age >= 55) {
ratioPerAge = 35
} else {
ratioPerAge = 30
}
const ounces = (kg * ratioPerAge) / 28.3
const cups = ounces / 8
return {
ounces: formatOunces(ounces),
liter: formatLiters(
ounceToLiter(ounces)
)
}
}
function main() {
console.log(glassesPerDay(AGE, WEIGHT))
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment