@@ -23,3 +23,41 @@ console.assert(
2323 currentOutput2 === targetOutput2 ,
2424 `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
2525) ;
26+
27+ // i put this function for test in console, chrome but it showed undefined then i put 12:00 and the output is:Uncaught SyntaxError:
28+ // Unexpected token.so the function is not workable and has some bugs.Unfortunately, i can not work out by myself, i went to chatGpt which
29+ // explained the detail to me. function format12HourClock(time) does not work right because it does not work for 12:00, 24:00 d not for
30+ // minutes either. The right function should be:
31+
32+ function formatAs12HourClock ( time ) {
33+ const hours24 = Number ( time . slice ( 0 , 2 ) ) ;
34+ const minutes = time . slice ( 3 ) ;
35+
36+ const period = hours24 >= 12 ? "pm" : "am" ;
37+ const hours12 = hours24 % 12 === 0 ? 12 : hours24 % 12 ;
38+
39+ return `${ String ( hours12 ) . padStart ( 2 , "0" ) } :${ minutes } ${ period } ` ;
40+ }
41+
42+ const currentOutput = formatAs12HourClock ( "08:00" ) ;
43+ const targetOutput = "08:00 am" ;
44+ console . assert (
45+ currentOutput === targetOutput ,
46+ `current output: ${ currentOutput } , target output: ${ targetOutput } `
47+ ) ;
48+
49+ const currentOutput2 = formatAs12HourClock ( "23:00" ) ;
50+ const targetOutput2 = "11:00 pm" ;
51+ console . assert (
52+ currentOutput2 === targetOutput2 ,
53+ `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
54+ ) ;
55+
56+ // const period = hours24 >= 12 ? "pm" : "am";This line uses a ternary operator — a concise way to write an if...else statement.
57+ // If hours24 is greater than or equal to 12, then set period to "pm".Otherwise, set it to "am".
58+
59+ // const hours12 = hours24 % 12 === 0 ? 12 : hours24 % 12;This is also a ternary operator (shortcut for if...else)
60+ // used to convert 24-hour time to 12-hour time format, which means:"If hours24 % 12 is 0, then set hours12 to 12,
61+ // otherwise set it to hours24 % 12." For me, i can understand now with chatgpt help.
62+
63+
0 commit comments