Skip to content

Instantly share code, notes, and snippets.

@CaptainGMan
Last active May 25, 2021 15:45
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 CaptainGMan/c72d8134b90b868a2925014b043d474b to your computer and use it in GitHub Desktop.
Save CaptainGMan/c72d8134b90b868a2925014b043d474b to your computer and use it in GitHub Desktop.
// Takes height in centimeters, converts it into meters and then squares the number.
const getHeight = function(height) {
const cent = height / 100;
const heightSquared = cent * cent;
return heightSquared;
}
//Converts feet and inches into centimeters.
const feetAndInches = function(heightFt, heightIn) {
const FtToInches = (heightFt * 12) + heightIn;
return FtToInches/ 0.39370;
}
//Takes weight and divides it by height squared to get the BMI number.
const bmiCalc = function(weight, height) {
const result = weight / getHeight(height);
return result.toFixed(2);
}
//Divides weight into the BMI --- this will get multiplied in bmiResult to get ideal weight.
const idealWeight = (bmi, weight) => weight / bmi;
//Get input
const vitalStatistics = function(weightKg, heightCm, heightFt, heightIn) {
if (heightCm) {
const height = getHeight(heightCm);
return bmiResult(weightKg, heightCm)
} else if (!heightCm) {
const height = feetAndInches(heightFt, heightIn);
return bmiResult(weightKg, height);
}
}
//Outputs the results and reccomended weights.
function bmiResult(weight, height) {
const bmi = bmiCalc(weight, height);
const bottomWeight = (idealWeight(bmi, weight) * 18.6)
const topWeight = (idealWeight(bmi, weight) * 24.9)
const message = `Your ideal weight is between ${Math.floor(bottomWeight)}kg and ${Math.floor(topWeight)}kg.`
if (bmi < 18.5) {
return `Your BMI is ${bmi} which means you are under weight. ${message}`
} else if (bmi <= 24.9) {
return `Your BMI is ${bmi} which means you are at a healthy weight. ${message}`
} else if (bmi <= 29.9) {
return `Your BMI is ${bmi} which means you are overweight. ${message}`
} else if (bmi >= 30) {
return `Your BMI is ${bmi} which means you are obese. ${message}`
} else {
return 'Please input a valid height and weight';
}
}
//Values are - weight KG, height Cm, height ft, heiht inches.
console.log(vitalStatistics(65,false ,5 ,5));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment