|
1 | | -// Below are the steps for how BMI is calculated |
2 | | - |
3 | | -// The BMI calculation divides an adult's weight in kilograms (kg) by their height in metres (m) squared. |
4 | | - |
5 | | -// For example, if you weigh 70kg (around 11 stone) and are 1.73m (around 5 feet 8 inches) tall, you work out your BMI by: |
6 | | - |
7 | | -// squaring your height: 1.73 x 1.73 = 2.99 |
8 | | -// dividing 70 by 2.99 = 23.41 |
9 | | -// Your result will be displayed to 1 decimal place, for example 23.4. |
10 | | - |
11 | | -// You will need to implement a function that calculates the BMI of someone based off their weight and height |
12 | | - |
13 | | -// Given someone's weight in kg and height in metres |
14 | | -// Then when we call this function with the weight and height |
15 | | -// It should return their Body Mass Index to 1 decimal place |
16 | | - |
17 | | -function calculateBMI(weight, height) { |
18 | | - // return the BMI of someone based off their weight and height |
19 | | -} |
20 | | - |
21 | 1 | // Predict and explain first... |
22 | | -// =============> write your prediction here |
23 | 2 | // I predict that the code will output 'undefined' because the function calculateBMI |
24 | 3 | // does not return any value yet. |
25 | 4 |
|
26 | | -// =============> write your explanation here |
27 | 5 | // The function calculateBMI currently has no return statement, so it will return 'undefined'. |
28 | 6 | // To fix this, we need to perform the BMI calculation: |
29 | 7 | // BMI = weight / (height * height) |
30 | 8 | // Then, we round the result to one decimal place using toFixed(1). |
31 | 9 |
|
32 | 10 | // Finally, correct the code to fix the problem |
33 | | -// =============> write your new code here |
34 | | - |
35 | 11 | function calculateBMI(weight, height) { |
36 | 12 | const bmi = weight / (height * height); |
37 | 13 | return bmi.toFixed(1); |
38 | 14 | } |
39 | 15 |
|
| 16 | +module.exports = calculateBMI; |
| 17 | + |
40 | 18 | // Example: |
41 | 19 | console.log(`The BMI of someone who weighs 70kg and is 1.73m tall is ${calculateBMI(70, 1.73)}`); |
42 | 20 | // Output: The BMI of someone who weighs 70kg and is 1.73m tall is 23.4 |
0 commit comments