Skip to content

Commit 9d65271

Browse files
committed
Complete Sprint 2 mandatory and stretch tasks
1 parent 871a197 commit 9d65271

File tree

5 files changed

+49
-23
lines changed

5 files changed

+49
-23
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@
1616

1717
function calculateBMI(weight, height) {
1818
// return the BMI of someone based off their weight and height
19-
}
19+
let bmi = weight / (height * height);
20+
return parseFloat(bmi.toFixed(1));
21+
}
22+
console.log(`The BMI is: ${calculateBMI(70, 1.73)}`);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@
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(text) {
18+
return text.toUpperCase().replaceAll(" ", "_");
19+
}
20+
console.log(toUpperSnakeCase("hello there"));
21+
console.log(toUpperSnakeCase("lord of the rings"));

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,16 @@
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+
function toPounds(penceString) {
8+
const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);
9+
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
10+
const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
11+
const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
12+
13+
return ${pounds}.${pence}`;
14+
}
15+
16+
console.log(toPounds("50p"));
17+
console.log(toPounds("399p"));
18+
console.log(toPounds("5p"));
19+
console.log(toPounds("12345p"));

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@ function formatTimeDisplay(seconds) {
1010

1111
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
1212
}
13-
13+
console.log(formatTimeDisplay(61));
1414
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
1515
// to help you answer these questions
1616

1717
// Questions
1818

1919
// a) When formatTimeDisplay is called how many times will pad be called?
20-
// =============> write your answer here
20+
// Answer: 3 times.
2121

2222
// Call formatTimeDisplay with an input of 61, now answer the following:
2323

2424
// b) What is the value assigned to num when pad is called for the first time?
25-
// =============> write your answer here
25+
// Answer: 0.
2626

2727
// c) What is the return value of pad is called for the first time?
28-
// =============> write your answer here
28+
// Answer: "00"
2929

3030
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
31-
// =============> write your answer here
31+
// Answer: 1. The last call evaluates `pad(remainingSeconds)`. Since 61 % 60 equals 1, the value of remainingSeconds passed to `num` is 1.
3232

3333
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
34-
// =============> write your answer here
34+
// Answer: "01". The function takes the number 1, converts it to a string, and pads it with a leading "0" to make it 2 characters long.

Sprint-2/5-stretch-extend/format-time.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,28 @@
33
// Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find.
44

55
function formatAs12HourClock(time) {
6-
const hours = Number(time.slice(0, 2));
7-
if (hours > 12) {
8-
return `${hours - 12}:00 pm`;
6+
let hours = Number(time.slice(0, 2));
7+
const minutes = time.slice(3, 5);
8+
9+
const period = hours >= 12 ? "pm" : "am";
10+
11+
hours = hours % 12;
12+
if (hours === 0) {
13+
hours = 12;
914
}
10-
return `${time} am`;
15+
16+
const paddedHours = hours.toString().padStart(2, "0");
17+
18+
return `${paddedHours}:${minutes} ${period}`;
1119
}
1220

13-
const currentOutput = formatAs12HourClock("08:00");
14-
const targetOutput = "08:00 am";
15-
console.assert(
16-
currentOutput === targetOutput,
17-
`current output: ${currentOutput}, target output: ${targetOutput}`
18-
);
21+
console.assert(formatAs12HourClock("08:00") === "08:00 am", "Morning time failed");
22+
console.assert(formatAs12HourClock("23:00") === "11:00 pm", "Night time failed");
1923

20-
const currentOutput2 = formatAs12HourClock("23:00");
21-
const targetOutput2 = "11:00 pm";
22-
console.assert(
23-
currentOutput2 === targetOutput2,
24-
`current output: ${currentOutput2}, target output: ${targetOutput2}`
25-
);
24+
console.assert(formatAs12HourClock("00:00") === "12:00 am", "Midnight edge case failed");
25+
console.assert(formatAs12HourClock("00:15") === "12:15 am", "Past midnight failed");
26+
27+
console.assert(formatAs12HourClock("12:00") === "12:00 pm", "Noon edge case failed");
28+
console.assert(formatAs12HourClock("12:30") === "12:30 pm", "Past noon failed");
29+
30+
console.assert(formatAs12HourClock("13:45") === "01:45 pm", "Padding hour formatting failed");

0 commit comments

Comments
 (0)