Skip to content

Commit 428849e

Browse files
solving time convert exercsie
1 parent 29ef74a commit 428849e

2 files changed

Lines changed: 111 additions & 9 deletions

File tree

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,77 @@
1+
// function formatAs12HourClock(time) {
2+
// //extract digits representing hours
3+
// const hours = Number(time.slice(0, 2));
4+
// //if houre value over 12, subtract 12
5+
// //if hour value under 12 , continue
6+
// if (time === "00:00") {
7+
// return `12:00 am`;
8+
// }
9+
// if (hours > 12) {
10+
// // add pm and reurn value
11+
// return `${hours - 12}:00 pm`;
12+
// }
13+
// //add am or pm
14+
// //return new value
15+
// return `${time} am`;
16+
// }
17+
118
function formatAs12HourClock(time) {
219

20+
console.log(time.length)
21+
22+
if (time.length == 4) {
23+
console.log("got here")
24+
time=time.padStart(5, "0")
25+
}
26+
27+
if (typeof time !== "string") {
28+
return "nccorect timing format";
29+
}
30+
31+
const match = time.match(/^(\d{1,2}):(\d{2})$/);
32+
33+
if (!match) {
34+
return "Inccorect timing format";
35+
}
36+
37+
38+
339
const hours = Number(time.slice(0, 2));
40+
const minutes = Number(time.slice(3, 5));
41+
console.log(hours + ` <------------look here hour`);
42+
console.log(time + ` <------------look here time`);
43+
44+
45+
if (time === "00:00") {
46+
return `12:00 am`;
47+
}
48+
if (time === "12:00") {
49+
return `12:00 pm`;
50+
}
51+
452

5-
if (hours > 12) {
6-
return `${hours - 12}:00 pm`;
53+
if (hours < 12) {
54+
return `${time} am`;
55+
} else if (hours == 12) {
56+
return `${time} am`;
57+
} else if (hours == 24) {
58+
return `12:00 am`;
59+
}
60+
61+
if (hours > 12 && hours < 22 && minutes < 10) {
62+
return `${(hours - 12).toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")} pm`;
63+
}
64+
if (hours > 12 && hours < 22 && minutes >= 10) {
65+
return `${(hours - 12).toString().padStart(2, "0")}:${minutes} pm`;
66+
}
67+
if (hours >= 22 && minutes < 10) {
68+
return `${hours - 12}:${minutes.toString().padStart(2, "0")} pm`;
69+
}
70+
if (hours >= 22 && minutes >= 10) {
71+
return `${hours - 12}:${minutes} pm`;
772
}
8-
return `${time} am`;
973
}
1074

11-
export {formatAs12HourClock};
75+
export { formatAs12HourClock };
76+
77+
Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,47 @@
1-
import {formatAs12HourClock} from "./timeConverter.js";
1+
import { formatAs12HourClock } from "./timeConverter.js";
22
import assert from "node:assert";
33
import test from "node:test";
44

5-
test("correctly convert time after 12:00", function(){
6-
assert.equal(formatAs12HourClock("23:00"), "11:00 pm");
5+
test("correctly convert time after 12:00", function () {
6+
assert.equal(formatAs12HourClock("23:00"), "11:00 pm");
77
});
88

9-
test("can correctly convert morning time", function() {
10-
assert.equal(formatAs12HourClock("08:00"), "08:00 am");
9+
test("can correctly convert morning time", function () {
10+
assert.equal(formatAs12HourClock("08:00"), "08:00 am");
1111
});
12+
13+
test("can corectly convert midnight", () =>
14+
assert.equal(formatAs12HourClock("00:00"), "12:00 am"));
15+
16+
test("can corectly convert mid-day", () =>
17+
assert.equal(formatAs12HourClock("12:00"), "12:00 pm"));
18+
19+
test("can corectly convert half an hour passed midnight", () =>
20+
assert.equal(formatAs12HourClock("00:30"), "00:30 am"));
21+
22+
test("can correctly convert time with missing leading zero", function () {
23+
assert.equal(formatAs12HourClock("9:00"), "09:00 am");
24+
});
25+
26+
test("can correctly handel non digit format entries", function () {
27+
assert.equal(formatAs12HourClock("nineOclock"), "Inccorect timing format");
28+
});
29+
/* ************************ Edge cases Minutes *****************************************************/
30+
/* over one hour <- 60 30 0 -> negative number */
31+
/* <----------------|---------------------------|---------------------------------------|-------------------> */
32+
/* **************************************************************************************************************/
33+
34+
/* ************************ Edge cases Hours *****************************************************/
35+
/* over one day <- 24 12 0 -> negative number */
36+
/* <----------------|---------------------------|---------------------------------------|-------------------> */
37+
/* **************************************************************************************************************/
38+
39+
/*--------------------------------------------|
40+
| Hours | Minures |
41+
| 00 | 00 |
42+
|___________________|___________________ ___|
43+
44+
45+
*
46+
47+
*/

0 commit comments

Comments
 (0)