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// The main function:
6- function formatAs12HourClock ( time ) {
7- const hours = Number ( time . slice ( 0 , 2 ) ) ;
8- if ( hours > 12 ) {
9- return `${ hours - 12 } :00 pm` ;
10- }
11- return `${ time } am` ;
12- }
6+ // function formatAs12HourClock(time) {
7+ // const hours = Number(time.slice(0, 2));
8+ // if (hours > 12) {
9+ // return `${hours - 12}:00 pm`;
10+ // }
11+ // return `${time} am`;
12+ // }
1313
1414// Tests:
15- const currentOutput = formatAs12HourClock ( "08:00" ) ;
16- const targetOutput = "08:00 am" ;
17- console . assert (
18- currentOutput === targetOutput ,
19- `current output: ${ currentOutput } , target output: ${ targetOutput } `
20- ) ; // It's output is correct
15+ // const currentOutput = formatAs12HourClock("08:00");
16+ // const targetOutput = "08:00 am";
17+ // console.assert(
18+ // currentOutput === targetOutput,
19+ // `current output: ${currentOutput}, target output: ${targetOutput}`
20+ // );
2121
22- const currentOutput2 = formatAs12HourClock ( "23:00" ) ;
23- const targetOutput2 = "11:00 pm" ;
24- console . assert (
25- currentOutput2 === targetOutput2 ,
26- `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
27- ) ; // It's output is correct
22+ // const currentOutput2 = formatAs12HourClock("23:00");
23+ // const targetOutput2 = "11:00 pm";
24+ // console.assert(
25+ // currentOutput2 === targetOutput2,
26+ // `current output: ${currentOutput2}, target output: ${targetOutput2}`
27+ // );
2828
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"
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+ // );
35+ // const currentOutput4 = formatAs12HourClock("00:00");
36+ // const targetOutput4 = "12:00 am";
37+ // console.assert(
38+ // currentOutput4 === targetOutput4,
39+ // `current output: ${currentOutput4}, target output: ${targetOutput4}`
40+ // );
41+ // const currentOutput5 = formatAs12HourClock("15:30");
42+ // const targetOutput5 = "3:30 pm";
43+ // console.assert(
44+ // currentOutput5 === targetOutput5,
45+ // `current output: ${currentOutput5}, target output: ${targetOutput5}`
46+ // );
3547
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
48+ // const currentOutput6 = formatAs12HourClock("11:45");
49+ // const targetOutput6 = "11:45 am";
50+ // console.assert(
51+ // currentOutput6 === targetOutput6,
52+ // `current output: ${currentOutput6}, target output: ${targetOutput6}`
53+ // );
5654
5755//Fixing the function:
5856function formatAs12HourClockFixed ( time ) {
@@ -78,39 +76,70 @@ const targetOutput1 = "8:00 am";
7876console . assert (
7977 currentOutput1 === targetOutput1 ,
8078 `current output: ${ currentOutput1 } , target output: ${ targetOutput1 } `
81- ) ; // It's output is correct
79+ ) ;
8280
83- const currentOutput2 = formatAs12HourClockFixed ( "23:00 " ) ;
84- const targetOutput2 = "11:00 pm " ;
81+ const currentOutput2 = formatAs12HourClockFixed ( "7:45 " ) ;
82+ const targetOutput2 = "7:45 am " ;
8583console . assert (
8684 currentOutput2 === targetOutput2 ,
8785 `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
88- ) ; // It's output is correct
86+ ) ;
8987
90- const currentOutput3 = formatAs12HourClockFixed ( "12 :00" ) ;
91- const targetOutput3 = "12 :00 pm" ;
88+ const currentOutput3 = formatAs12HourClockFixed ( "23 :00" ) ;
89+ const targetOutput3 = "11 :00 pm" ;
9290console . assert (
9391 currentOutput3 === targetOutput3 ,
9492 `current output: ${ currentOutput3 } , target output: ${ targetOutput3 } `
95- ) ; // It's output is correct
93+ ) ;
9694
97- const currentOutput4 = formatAs12HourClockFixed ( "00 :00" ) ;
98- const targetOutput4 = "12:00 am " ;
95+ const currentOutput4 = formatAs12HourClockFixed ( "12 :00" ) ;
96+ const targetOutput4 = "12:00 pm " ;
9997console . assert (
10098 currentOutput4 === targetOutput4 ,
10199 `current output: ${ currentOutput4 } , target output: ${ targetOutput4 } `
102- ) ; // It's output is correct
100+ ) ;
103101
104- const currentOutput5 = formatAs12HourClockFixed ( "15:30 " ) ;
105- const targetOutput5 = "3:30 pm " ;
102+ const currentOutput5 = formatAs12HourClockFixed ( "00:00 " ) ;
103+ const targetOutput5 = "12:00 am " ;
106104console . assert (
107105 currentOutput5 === targetOutput5 ,
108106 `current output: ${ currentOutput5 } , target output: ${ targetOutput5 } `
109- ) ; // It's output is correct
107+ ) ;
110108
111- const currentOutput6 = formatAs12HourClockFixed ( "11:45 " ) ;
112- const targetOutput6 = "11:45 am " ;
109+ const currentOutput6 = formatAs12HourClockFixed ( "15:30 " ) ;
110+ const targetOutput6 = "3:30 pm " ;
113111console . assert (
114112 currentOutput6 === targetOutput6 ,
115113 `current output: ${ currentOutput6 } , target output: ${ targetOutput6 } `
116- ) ; // It's output is correct
114+ ) ;
115+
116+ const currentOutput7 = formatAs12HourClockFixed ( "11:45" ) ;
117+ const targetOutput7 = "11:45 am" ;
118+ console . assert (
119+ currentOutput7 === targetOutput7 ,
120+ `current output: ${ currentOutput7 } , target output: ${ targetOutput7 } `
121+ ) ;
122+
123+ const currentOutput8 = formatAs12HourClockFixed ( "25:30" ) ;
124+ const targetOutput8 = "Invalid time" ;
125+ console . assert (
126+ currentOutput8 === targetOutput8 ,
127+ `current output: ${ currentOutput8 } , target output: ${ targetOutput8 } `
128+ ) ;
129+
130+ const currentOutput9 = formatAs12HourClockFixed ( "1724" ) ;
131+ const targetOutput9 = "Invalid time" ;
132+ console . assert (
133+ currentOutput9 === targetOutput9 ,
134+ `current output: ${ currentOutput9 } , target output: ${ targetOutput9 } `
135+ ) ;
136+ console . log ( formatAs12HourClockFixed ( "08:00" ) ) ;
137+
138+ console . log ( formatAs12HourClockFixed ( "7:45" ) ) ;
139+ console . log ( formatAs12HourClockFixed ( "23:00" ) ) ;
140+ console . log ( formatAs12HourClockFixed ( "12:00" ) ) ;
141+ console . log ( formatAs12HourClockFixed ( "00:00" ) ) ;
142+ console . log ( formatAs12HourClockFixed ( "15:30" ) ) ;
143+ console . log ( formatAs12HourClockFixed ( "11:45" ) ) ;
144+ console . log ( formatAs12HourClockFixed ( "25:30" ) ) ;
145+ console . log ( formatAs12HourClockFixed ( "1724688" ) ) ;
0 commit comments