-
-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathformat-time.js
More file actions
76 lines (65 loc) · 2.5 KB
/
Copy pathformat-time.js
File metadata and controls
76 lines (65 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// This is the latest solution to the problem from the prep.
// Make sure to do the prep before you do the coursework
// 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.
/*function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
if (hours > 12) {
return `${hours - 12}:00 pm`;
}
return `${time} am`;
}
const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
);
const currentOutput2 = formatAs12HourClock("23:00");
const targetOutput2 = "11:00 pm";
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
); */
// The function ignores the minutes and did'not handle midnight (00:00) or noon (12:00) properly.
// Better version:
function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
const minutes = time.slice(3, 5);
if (hours == 0) return `12:${minutes} am`;
if (hours < 12) return `${hours}:${minutes} am`;
if (hours === 12) return `${time} pm`;
return `${hours - 12}:${minutes} pm`;
}
const currentOutput1 = formatAs12HourClock("08:00");
const targetOutput1 = "08:00 am";
console.assert(
currentOutput1 === targetOutput1,
`current output: ${currentOutput1}, target output: ${targetOutput1}`
);
// Test failed: due to a typo
const currentOutput2 = formatAs12HourClock("23:00");
const targetOutput2 = "11:00 pm";
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);
const currentOutput3 = formatAs12HourClock("13:00");
const targetOutput3 = "1:00 pm";
console.assert(
currentOutput3 === targetOutput3,
`current output: ${currentOutput3} target output: {targetOutput3}`
);
const currentOutput4 = formatAs12HourClock("15:00");
const targetOutput4 = "3:00 pm";
console.assert(
currentOutput4 === targetOutput4,
`current output: ${currentOutput4} target output: {targetOutput4}`
);
console.log(formatAs12HourClock("08:00"));
console.log(formatAs12HourClock("23:00"));
console.log(formatAs12HourClock("15:00"));
console.assert(formatAs12HourClock("00:00") === "12:00 am");
console.assert(formatAs12HourClock("12:00") === "12:00 pm");
// The test passed, so my function gave the expected results.
// All the console.assert() tests passed.
// formatAs12HourClock() function works correctly for the inputs I tested.