Dark Mode
Enable Console
bmi(weight, height)
/*
Calculates your BMI and returns a weight category
*/
bmi(170, 2.8);
// 'Normal'
bmi(120, 2.8);
// 'Underweight'
Solution by - Need4Swede
function bmi(weight, height) {
// Your BMI is weight / height squared
let result = weight / height ** 2;
// Our array of results
let weightClasses = [
18.5, 'Underweight',
25.0, 'Normal',
30.0, 'Overweight',
30.1, 'Obese'
];
// If you've reached the highest BMI number, return the last weight class
for (let i = 0; i < weightClasses.length; i+=2) {
if (i === weightClasses.length - 2) {
return weightClasses[weightClasses.length - 1];
}
// Return the name of the appropriate weight class
if (result < weightClasses[i]) {
return weightClasses[i+1];
}
}
}
Try it yourself - Codewars Source