1- // This is the latest solution to the problem from the prep.
2- // Make sure to do the prep before you do the coursework
3- // 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.
4-
5- // 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- //}
13-
14- // 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- // );
21-
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- // );
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- // );
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- // );
47-
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- // );
54-
55- //Fixing the function:
56- function formatAs12HourClockFixed ( time ) {
57- const hours = Number ( time . slice ( 0 , 2 ) ) ;
58- const minutes = time . slice ( 3 , 5 ) ;
1+ // Fixed function
2+ function formatAs12HourClock ( time ) {
3+ // Validate input format
4+ if ( typeof time !== "string" || ! time . includes ( ":" ) ) {
5+ return "Invalid time" ;
6+ }
7+
8+ const [ hourStr , minuteStr ] = time . split ( ":" ) ;
9+ const hours = Number ( hourStr ) ;
10+ const minutes = Number ( minuteStr ) ;
5911 let period = "am" ;
6012
13+ // Validate range
14+ if (
15+ isNaN ( hours ) || isNaN ( minutes ) ||
16+ hours < 0 || hours > 23 ||
17+ minutes < 0 || minutes > 59
18+ ) {
19+ return "Invalid time" ;
20+ }
21+
22+ // Midnight
6123 if ( hours === 0 ) {
62- return `12:${ minutes } am` ;
24+ return `12:${ minuteStr . padStart ( 2 , "0" ) } am` ;
6325 }
26+
27+ // Noon
6428 if ( hours === 12 ) {
6529 period = "pm" ;
30+ return `12:${ minuteStr . padStart ( 2 , "0" ) } ${ period } ` ;
6631 }
32+
33+ // PM hours
6734 if ( hours > 12 ) {
68- return `${ hours - 12 } :${ minutes } pm` ;
35+ return `${ hours - 12 } :${ minuteStr . padStart ( 2 , "0" ) } pm` ;
6936 }
70- return `${ hours } :${ minutes } ${ period } ` ;
37+
38+ // AM hours
39+ return `${ hours } :${ minuteStr . padStart ( 2 , "0" ) } ${ period } ` ;
7140}
7241
73- // Re-testing :
74- const currentOutput1 = formatAs12HourClockFixed ( "08:00" ) ;
42+ // ✅ Re-tests
43+ const currentOutput1 = formatAs12HourClock ( "08:00" ) ;
7544const targetOutput1 = "8:00 am" ;
7645console . assert (
7746 currentOutput1 === targetOutput1 ,
7847 `current output: ${ currentOutput1 } , target output: ${ targetOutput1 } `
7948) ;
8049
81- const currentOutput2 = formatAs12HourClockFixed ( "7:45" ) ;
50+ const currentOutput2 = formatAs12HourClock ( "7:45" ) ;
8251const targetOutput2 = "7:45 am" ;
8352console . assert (
8453 currentOutput2 === targetOutput2 ,
8554 `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
8655) ;
8756
88- const currentOutput3 = formatAs12HourClockFixed ( "23:00" ) ;
57+ const currentOutput3 = formatAs12HourClock ( "23:00" ) ;
8958const targetOutput3 = "11:00 pm" ;
9059console . assert (
9160 currentOutput3 === targetOutput3 ,
9261 `current output: ${ currentOutput3 } , target output: ${ targetOutput3 } `
9362) ;
9463
95- const currentOutput4 = formatAs12HourClockFixed ( "12:00" ) ;
64+ const currentOutput4 = formatAs12HourClock ( "12:00" ) ;
9665const targetOutput4 = "12:00 pm" ;
9766console . assert (
9867 currentOutput4 === targetOutput4 ,
9968 `current output: ${ currentOutput4 } , target output: ${ targetOutput4 } `
10069) ;
10170
102- const currentOutput5 = formatAs12HourClockFixed ( "00:00" ) ;
71+ const currentOutput5 = formatAs12HourClock ( "00:00" ) ;
10372const targetOutput5 = "12:00 am" ;
10473console . assert (
10574 currentOutput5 === targetOutput5 ,
10675 `current output: ${ currentOutput5 } , target output: ${ targetOutput5 } `
10776) ;
10877
109- const currentOutput6 = formatAs12HourClockFixed ( "15:30" ) ;
78+ const currentOutput6 = formatAs12HourClock ( "15:30" ) ;
11079const targetOutput6 = "3:30 pm" ;
11180console . assert (
11281 currentOutput6 === targetOutput6 ,
11382 `current output: ${ currentOutput6 } , target output: ${ targetOutput6 } `
11483) ;
11584
116- const currentOutput7 = formatAs12HourClockFixed ( "11:45" ) ;
85+ const currentOutput7 = formatAs12HourClock ( "11:45" ) ;
11786const targetOutput7 = "11:45 am" ;
11887console . assert (
11988 currentOutput7 === targetOutput7 ,
12089 `current output: ${ currentOutput7 } , target output: ${ targetOutput7 } `
12190) ;
12291
123- const currentOutput8 = formatAs12HourClockFixed ( "25:30" ) ;
92+ const currentOutput8 = formatAs12HourClock ( "25:30" ) ;
12493const targetOutput8 = "Invalid time" ;
12594console . assert (
12695 currentOutput8 === targetOutput8 ,
12796 `current output: ${ currentOutput8 } , target output: ${ targetOutput8 } `
12897) ;
12998
130- const currentOutput9 = formatAs12HourClockFixed ( "1724 ") ;
99+ const currentOutput9 = formatAs12HourClock ( "1724688 ") ;
131100const targetOutput9 = "Invalid time" ;
132101console . assert (
133102 currentOutput9 === targetOutput9 ,
134103 `current output: ${ currentOutput9 } , target output: ${ targetOutput9 } `
135104) ;
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" ) ) ;
105+
106+ // ✅ Display output
107+ console . log ( formatAs12HourClock ( "08:00" ) ) ;
108+ console . log ( formatAs12HourClock ( "7:45" ) ) ;
109+ console . log ( formatAs12HourClock ( "23:00" ) ) ;
110+ console . log ( formatAs12HourClock ( "12:00" ) ) ;
111+ console . log ( formatAs12HourClock ( "00:00" ) ) ;
112+ console . log ( formatAs12HourClock ( "15:30" ) ) ;
113+ console . log ( formatAs12HourClock ( "11:45" ) ) ;
114+ console . log ( formatAs12HourClock ( "25:30" ) ) ;
115+ console . log ( formatAs12HourClock ( "1724688" ) ) ;
0 commit comments