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+ // The main function:
56function formatAs12HourClock ( time ) {
67 const hours = Number ( time . slice ( 0 , 2 ) ) ;
78 if ( hours > 12 ) {
@@ -10,16 +11,106 @@ function formatAs12HourClock(time) {
1011 return `${ time } am` ;
1112}
1213
14+ // Tests:
1315const currentOutput = formatAs12HourClock ( "08:00" ) ;
1416const targetOutput = "08:00 am" ;
1517console . assert (
1618 currentOutput === targetOutput ,
1719 `current output: ${ currentOutput } , target output: ${ targetOutput } `
18- ) ;
20+ ) ; // It's output is correct
1921
2022const currentOutput2 = formatAs12HourClock ( "23:00" ) ;
2123const targetOutput2 = "11:00 pm" ;
2224console . assert (
2325 currentOutput2 === targetOutput2 ,
2426 `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
25- ) ;
27+ ) ; // It's output is correct
28+
29+ const currentOutput3 = formatAs12HourClock ( "12:00" ) ;
30+ const targetOutput3 = "12:00 pm" ;
31+ console . assert (
32+ currentOutput3 === targetOutput3 ,
33+ `current output: ${ currentOutput3 } , target output: ${ targetOutput3 } `
34+ ) ; // It's output is incorrect because it returns "12:00 am" instead of "12:00 pm"
35+
36+ const currentOutput4 = formatAs12HourClock ( "00:00" ) ;
37+ const targetOutput4 = "12:00 am" ;
38+ console . assert (
39+ currentOutput4 === targetOutput4 ,
40+ `current output: ${ currentOutput4 } , target output: ${ targetOutput4 } `
41+ ) ; // It's output is incorrect because it returns "00:00 am" instead of "12:00 am"
42+
43+ const currentOutput5 = formatAs12HourClock ( "15:30" ) ;
44+ const targetOutput5 = "3:30 pm" ;
45+ console . assert (
46+ currentOutput5 === targetOutput5 ,
47+ `current output: ${ currentOutput5 } , target output: ${ targetOutput5 } `
48+ ) ; // It's output is incorrect because it returns "15:00 pm" instead of "3:30 pm"
49+
50+ const currentOutput6 = formatAs12HourClock ( "11:45" ) ;
51+ const targetOutput6 = "11:45 am" ;
52+ console . assert (
53+ currentOutput6 === targetOutput6 ,
54+ `current output: ${ currentOutput6 } , target output: ${ targetOutput6 } `
55+ ) ; // It's output is correct
56+
57+ //Fixing the function:
58+ function formatAs12HourClockFixed ( time ) {
59+ const hours = Number ( time . slice ( 0 , 2 ) ) ;
60+ const minutes = time . slice ( 3 , 5 ) ;
61+ let period = "am" ;
62+
63+ if ( hours === 0 ) {
64+ return `12:${ minutes } am` ;
65+ }
66+ if ( hours === 12 ) {
67+ period = "pm" ;
68+ }
69+ if ( hours > 12 ) {
70+ return `${ hours - 12 } :${ minutes } pm` ;
71+ }
72+ return `${ hours } :${ minutes } ${ period } ` ;
73+ }
74+
75+ // Re-testing :
76+ const currentOutput1 = formatAs12HourClockFixed ( "08:00" ) ;
77+ const targetOutput1 = "8:00 am" ;
78+ console . assert (
79+ currentOutput1 === targetOutput1 ,
80+ `current output: ${ currentOutput1 } , target output: ${ targetOutput1 } `
81+ ) ; // It's output is correct
82+
83+ const currentOutput2 = formatAs12HourClockFixed ( "23:00" ) ;
84+ const targetOutput2 = "11:00 pm" ;
85+ console . assert (
86+ currentOutput2 === targetOutput2 ,
87+ `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
88+ ) ; // It's output is correct
89+
90+ const currentOutput3 = formatAs12HourClockFixed ( "12:00" ) ;
91+ const targetOutput3 = "12:00 pm" ;
92+ console . assert (
93+ currentOutput3 === targetOutput3 ,
94+ `current output: ${ currentOutput3 } , target output: ${ targetOutput3 } `
95+ ) ; // It's output is correct
96+
97+ const currentOutput4 = formatAs12HourClockFixed ( "00:00" ) ;
98+ const targetOutput4 = "12:00 am" ;
99+ console . assert (
100+ currentOutput4 === targetOutput4 ,
101+ `current output: ${ currentOutput4 } , target output: ${ targetOutput4 } `
102+ ) ; // It's output is correct
103+
104+ const currentOutput5 = formatAs12HourClockFixed ( "15:30" ) ;
105+ const targetOutput5 = "3:30 pm" ;
106+ console . assert (
107+ currentOutput5 === targetOutput5 ,
108+ `current output: ${ currentOutput5 } , target output: ${ targetOutput5 } `
109+ ) ; // It's output is correct
110+
111+ const currentOutput6 = formatAs12HourClockFixed ( "11:45" ) ;
112+ const targetOutput6 = "11:45 am" ;
113+ console . assert (
114+ currentOutput6 === targetOutput6 ,
115+ `current output: ${ currentOutput6 } , target output: ${ targetOutput6 } `
116+ ) ; // It's output is correct
0 commit comments