Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sethschori/ee4a1010d49ee7ee830a27b399f9e692 to your computer and use it in GitHub Desktop.
Save sethschori/ee4a1010d49ee7ee830a27b399f9e692 to your computer and use it in GitHub Desktop.
https://repl.it/CTTj/114 created by sethopia
/*
BLOOD ALCOHOL CALC
An individual's BAC may be calculated as follows:
BAC% = (A × 5.14 / W × r) - 0.15 × H
A: Total alcohol consumed, in ounces (oz)
W: Body weight, in pounds (lbs)
r: The alcohol distribution ratio, 0.73 for man, and 0.66 for women
H: Time passed since drinking, in hours
create a function getBAC(weight, gender, drinks, drinkType, hours) that takes the following:
weight - a person's body weight,
gender - their gender as a string "M" or "F",
drinks - the number of drinks they've had
drinkType - a string "beer" or "whiskey" representing the type of drinks
hours - hours since last drink
Note:
Beers are 8oz and 4% alcohol by volume
Whiskeys are 4oz and 32% alcohol by volume
getBAC(125, "F", 4, "whiskey", 1) ==> 0.16899151515151514
getBAC(160, "M", 22, "beer", 2) ==> 0.009808219178082223
getBAC(400, "M", 1, "whiskey", 3) ==> 0
*/
function getBAC(weight, gender, drinks, drinkType, hours) {
var alcohol = 0;
var ratio = 0;
if (drinkType === "beer") {
alcohol = drinks * 8 * .04;
} else if (drinkType === "whiskey") {
alcohol = drinks * 4 * .32;
}
if (gender === "M") {
ratio = .73;
} else if (gender === "F") {
ratio = .66;
}
if (((alcohol * 5.14) / (weight * ratio)) - (0.15 * hours) < 0) {
return 0;
}
return ((alcohol * 5.14) / (weight * ratio)) - (0.15 * hours);
}
console.log(getBAC(125, "F", 4, "whiskey", 1));
console.log(getBAC(160, "M", 22, "beer", 2));
console.log(getBAC(400, "M", 1, "whiskey", 3));
Native Browser JavaScript
>>> 0.16899151515151514
0.009808219178082223
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment