Skip to content

Commit feee6ba

Browse files
committed
Implement calculateBMI function to compute BMI based on weight and height, including error handling for invalid height
1 parent 91327e6 commit feee6ba

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

‎Sprint-3/3-mandatory-implement/1-bmi.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,12 @@
1515
// It should return a string of their Body Mass Index to 1 decimal place
1616

1717
function calculateBMI(weight, height) {
18+
if (!height || height <= 0){ // to catch cases of undefined and null
19+
throw new Error('Height must be greater than 0');
20+
}
21+
const heightSquared = height ** 2;
22+
const bmi = weight / heightSquared;
23+
return bmi.toFixed(1); // rounds 'bmi' to 1 decimal place and converts to a string
1824
// return the BMI of someone based off their weight and height
1925
}
26+
console.log(`The Body Mass Index is: ${calculateBMI(70, 1.73)}`);

0 commit comments

Comments
 (0)