File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11function formatAs12HourClock ( time ) {
2-
32 const hours = Number ( time . slice ( 0 , 2 ) ) ;
3+ const minutes = time . slice ( 3 ) ;
4+
5+ if ( hours === 0 ) {
6+ return `12:${ minutes } am` ;
7+ }
48
59 if ( hours > 12 ) {
6- return `${ hours - 12 } :00 pm` ;
10+ return `${ hours - 12 } :${ minutes } pm` ;
711 }
12+
13+ if ( hours === 12 ) {
14+ return `${ time } pm` ;
15+ }
16+
817 return `${ time } am` ;
918}
1019
Original file line number Diff line number Diff line change 1- import { formatAs12HourClock } from "./timeConverter.js" ;
2- import assert from "node:assert" ;
1+ import { formatAs12HourClock } from "./timeConverter.js" ;
2+ import assert from "node:assert/strict " ;
33import test from "node:test" ;
44
5- test ( "correctly convert time after 12:00" , function ( ) {
6- assert . equal ( formatAs12HourClock ( "23:00" ) , "11:00 pm" ) ;
7- } ) ;
5+ const cases = [
6+ [ "00:00" , "12:00 am" ] ,
7+ [ "00:01" , "12:01 am" ] ,
8+ [ "01:05" , "01:05 am" ] ,
9+ [ "08:00" , "08:00 am" ] ,
10+ [ "11:59" , "11:59 am" ] ,
11+ [ "12:00" , "12:00 pm" ] ,
12+ [ "12:34" , "12:34 pm" ] ,
13+ [ "13:00" , "1:00 pm" ] ,
14+ [ "15:45" , "3:45 pm" ] ,
15+ [ "23:00" , "11:00 pm" ] ,
16+ [ "23:59" , "11:59 pm" ] ,
17+ ] ;
818
9- test ( "can correctly convert morning time" , function ( ) {
10- assert . equal ( formatAs12HourClock ( "08:00" ) , "08:00 am" ) ;
11- } ) ;
19+ for ( const [ time , expected ] of cases ) {
20+ test ( `${ time } converts to ${ expected } ` , ( ) => {
21+ assert . equal ( formatAs12HourClock ( time ) , expected ) ;
22+ } ) ;
23+ }
You can’t perform that action at this time.
0 commit comments