Skip to content

Commit fb06b74

Browse files
committed
Fix time formatting logic in formatAs12HourClock function; update test cases for accuracy
1 parent 4e6c099 commit fb06b74

2 files changed

Lines changed: 21 additions & 9 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ function formatTimeDisplay(seconds) {
2626
// Call formatTimeDisplay with an input of 61, now answer the following:
2727

2828
// b) What is the value assigned to num when pad is called for the first time?
29-
// =============> 1
29+
// =============> 0
3030

3131
// c) What is the return value of pad is called for the first time?
32-
// =============> "01"
32+
// =============> 00
3333

3434
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
35-
// =============> write your answer here
35+
// =============> 1, the modulo % operator means that 61 gives a remainder of 1
3636

3737
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
38-
// =============> write your answer here
38+
// =============> 01, this is so the 1 is padded with a leading 0 to make it 2 characters long.

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@
22
// Make sure to do the prep before you do the coursework
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

5+
const pad = (num) => String(num).padStart(2, "0");
6+
57
function formatAs12HourClock(time) {
68
const hours = Number(time.slice(0, 2));
7-
if (hours > 12) {
8-
return `${hours - 12}:00 pm`;
9+
const minutes = time.slice(3, 5);
10+
11+
if (hours === 0) {
12+
return `12:${minutes} am`;
13+
}
14+
else if (hours > 12) {
15+
return `${pad(hours - 12)}:${minutes} pm`;
16+
}
17+
else if (hours === 12) {
18+
return `${hours}:${minutes} pm`;
19+
}
20+
else {
21+
return `${pad(hours)}:${minutes} am`;
922
}
10-
return `${time} am`;
1123
}
1224

1325
const currentOutput = formatAs12HourClock("08:00");
@@ -25,14 +37,14 @@ console.assert(
2537
);
2638

2739
const currentOutput3 = formatAs12HourClock("13:15");
28-
const targetOutput3 = "1:15 pm";
40+
const targetOutput3 = "01:15 pm";
2941
console.assert(
3042
currentOutput3 === targetOutput3,
3143
`current output: ${currentOutput3}, target output: ${targetOutput3}`
3244
);
3345

3446
const currentOutput4 = formatAs12HourClock("12:00");
35-
const targetOutput4 = "12:00 am";
47+
const targetOutput4 = "12:00 pm";
3648
console.assert(
3749
currentOutput4 === targetOutput4,
3850
`current output: ${currentOutput4}, target output: ${targetOutput4}`

0 commit comments

Comments
 (0)