Skip to content
This repository was archived by the owner on Mar 1, 2026. It is now read-only.

Commit 3f1a344

Browse files
authored
W1/3 - Time Conversion (TypeScript) (#136)
I was a in a bit of hurry last night, so I've sharpened up the first TypeScript solution. Correct JSDocs. Fixed a bug in the solution where `12:00:00AM` was not returning `00:00:00`
1 parent 1b5f369 commit 3f1a344

3 files changed

Lines changed: 24 additions & 6 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# [Hackerrank 90 days](https://www.hackerrank.com/interview/preparation-kits/three-month-preparation-kit/three-month-week-three/challenges)
22

33

4-
Don't judge the layout of this repository in terms of "that's not how you lay out an Elixir project, or a Go project or a ...". Feel free to judge me on the code in this repository. Python was my strongest language when I started this and I think that shows. I'm hoping by the time I am at the end of it all, it's harder to tell what's my strongest language.
4+
This repo’s layout isn’t idiomatic to any one language — Python was my strongest when I started, and it probably shows. I’m aiming to make that less obvious as I go through the challenges that by the end.
55

6-
If you spot anything that you think I could do better, open a pull request. That'd be lovely
6+
Spot something I could improve? A pull request would be lovely.
77

88

99
## 🙋 Why?

w1/tc/3_1.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
12:40:22AM
2+
3+
00:40:22

w1/time_conversion.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
1+
/**
2+
* Time Conversion - Challenge 3 - Week 1
3+
* https://www.hackerrank.com/challenges/three-month-preparation-kit-time-conversion/problem
4+
*
5+
* @param s - A time string in the form 12:40:22AM
6+
* @returns - A 'military' formatted time 00:40:22
7+
*
8+
* @example
9+
* timeConversion("12:40:22AM") // "00:40:22"
10+
* timeConversion("07:05:45PM") // "19:05:45"
11+
*/
112
export function timeConversion(s:string): string {
2-
console.log(s)
313
// get into parts
414
const [, hourStr, minStr, secStr, period] =
515
s.match(/^(\d{2}):(\d{2}):(\d{2})(AM|PM)$/)!;
616

7-
// if last is PM add 12 to hour
17+
// hour is the target of our meddling.
818
let hour = parseInt(hourStr);
19+
20+
// if period is PM add 12 to hour.
921
if (period === "PM" && hour !== 12) hour += 12;
1022

11-
// fomat and return
12-
return `${hour}:${minStr}:${secStr}`
23+
// if period is AM and hour is twelve, then it moves to zero.
24+
if (period === "AM" && hour === 12) hour = 0;
25+
26+
// format and return
27+
return `${hour.toString().padStart(2, "0")}:${minStr}:${secStr}`;
1328
}

0 commit comments

Comments
 (0)