Skip to content

Commit 5a7c70d

Browse files
committed
Implement BMI calculation and UPPER_SNAKE_CASE conversion functions; add test cases for validation
1 parent c09112f commit 5a7c70d

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

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

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

1717
function calculateBMI(weight, height) {
18-
// return the BMI of someone based off their weight and height
19-
}
18+
// return the BMI of someone based off their weight and height
19+
const bmi = weight / (height * height);
20+
return parseFloat(bmi.toFixed(1));
21+
}
22+
console.log(calculateBMI(70, 1.73)); // This line is inserted only to test the function. It returns 23.4
23+
console.log(calculateBMI(80, 1.8)); // This line is inserted only to test the function. It returns 24.7
24+
console.log(calculateBMI(90, 1.75)); // This line is inserted only to test the function. It returns 29.4

Sprint-2/3-mandatory-implement/2-cases.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,9 @@
1414
// You will need to come up with an appropriate name for the function
1515
// Use the MDN string documentation to help you find a solution
1616
// This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase
17+
function toUpperSnakeCase(str) {
18+
// return the string in UPPER_SNAKE_CASE
19+
return str.toUpperCase().replace(/ /g, "_");
20+
}
21+
console.log(toUpperSnakeCase("hello there")); // This line is inserted only to test the function. It returns "HELLO_THERE"
22+
console.log(toUpperSnakeCase("lord of the rings")); // This line is inserted only to test the function. It returns "LORD_OF_THE_RINGS"

Sprint-2/3-mandatory-implement/3-to-pounds.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,28 @@
44
// You will need to declare a function called toPounds with an appropriately named parameter.
55

66
// You should call this function a number of times to check it works for different inputs
7+
8+
function toPounds(penceString) {
9+
// Convert a string representing an amount in pence (e.g., "123p") to pounds and pence format (e.g., "£1.23")
10+
11+
const penceStringWithoutTrailingP = penceString.substring(
12+
0,
13+
penceString.length - 1
14+
);
15+
16+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
17+
const pounds = paddedPenceNumberString.substring(
18+
0,
19+
paddedPenceNumberString.length - 2
20+
);
21+
22+
const pence = paddedPenceNumberString
23+
.substring(paddedPenceNumberString.length - 2)
24+
.padEnd(2, "0");
25+
26+
return ${pounds}.${pence}`;
27+
}
28+
console.log(toPounds("5555p")); // This line is inserted only to test the function. It returns "£55.55"
29+
console.log(toPounds("399p")); // This line is inserted only to test the function. It returns "£0.05"
30+
console.log(toPounds("50p")); // This line is inserted only to test the function. It returns "£0.50"
31+
console.log(toPounds("500p")); // This line is inserted only to test the function. It returns "£5.00"

Sprint-2/4-mandatory-interpret/time-format.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,22 @@ function formatTimeDisplay(seconds) {
1010

1111
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
1212
}
13+
console.log(formatTimeDisplay(61)); // This line is inserted only to test the function. It returns "00:01:01"
1314

15+
// Here is a function that takes a number of seconds and returns a string formatted as "HH:MM:SS"
1416
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
1517
// to help you answer these questions
1618

1719
// Questions
1820

1921
// a) When formatTimeDisplay is called how many times will pad be called?
2022
// =============> write your answer here
21-
23+
//3 times
2224
// Call formatTimeDisplay with an input of 61, now answer the following:
23-
25+
// 00:01:01
2426
// b) What is the value assigned to num when pad is called for the first time?
2527
// =============> write your answer here
26-
28+
//61
2729
// c) What is the return value of pad is called for the first time?
2830
// =============> write your answer here
2931

0 commit comments

Comments
 (0)