|
1 | | -// In Sprint-1, there is a program written in interpret/to-pounds.js |
| 1 | +// Below are the steps for how BMI is calculated |
2 | 2 |
|
3 | | -// You will need to take this code and turn it into a reusable block of code. |
4 | | -// You will need to declare a function called toPounds with an appropriately named parameter. |
| 3 | +// The BMI calculation divides an adult's weight in kilograms (kg) by their height in metres (m) squared. |
5 | 4 |
|
6 | | -// You should call this function a number of times to check it works for different inputs |
| 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: |
7 | 6 |
|
8 | | -function calculateBMI(penceString) { |
| 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. |
9 | 10 |
|
10 | | - // remove last character |
11 | | - const penceStringWithoutTrailingP = penceString.substring( |
12 | | - 0, |
13 | | - penceString.length - 1 |
14 | | - ); |
| 11 | +// You will need to implement a function that calculates the BMI of someone based off their weight and height |
15 | 12 |
|
16 | | - // pad to the length of three |
17 | | - const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); |
| 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 |
18 | 16 |
|
19 | | - // remove the last two characters |
20 | | - const pounds = paddedPenceNumberString.substring( |
21 | | - 0, |
22 | | - paddedPenceNumberString.length - 2 |
23 | | - ); |
24 | | - |
25 | | - // get last two characters and pad to two if needed |
26 | | - const pence = paddedPenceNumberString |
27 | | - .substring(paddedPenceNumberString.length - 2) |
28 | | - .padEnd(2, "0"); |
29 | | - |
30 | | - console.log(`£${pounds}.${pence}`); |
31 | | - |
| 17 | +function calculateBMI(weight, height) { |
| 18 | + // return the BMI of someone based off their weight and height |
| 19 | + return (weight/(height**2)).toFixed(1) |
32 | 20 | } |
33 | 21 |
|
34 | | -console.log(calculateBMI("456p")) |
35 | | -console.log(calculateBMI("545747p")) |
36 | | -console.log(calculateBMI("1p")) |
| 22 | +console.log (calculateBMI(70, 1.73)); |
| 23 | +} |
0 commit comments