-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat-time.js
More file actions
81 lines (71 loc) · 2.65 KB
/
Copy pathformat-time.js
File metadata and controls
81 lines (71 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// This is the latest solution to the problem from the prep.
// Make sure to do the prep before you do the coursework
// 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.
// --- > Debugging this code was not easy for me, I could solve this by the help of AI Explaining each step and conditions and finally we made it work well with different inputs, I mean string with number character.
/*function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
const minutes = time.slice(2);
let formattedHours;
if (hours === 0) {
formattedHours = 12;
} else if (hours > 12) {
formattedHours = hours - 12;
} else {
formattedHours = hours;
}
// ---> Add leading zero if needed
if (formattedHours < 10) {
formattedHours = "0" + formattedHours;
}
if (hours >= 12) {
return `${formattedHours}${minutes} pm`;
} else {
return `${formattedHours}${minutes} am`;
}
} */
function formatAs12HourClock(time) {
let parts = time.split(":");
let hours = Number(parts[0]);
let minutes = parts[1];
let period = "am";
if (hours >= 12) {
period = "pm";
}
if (hours === 0) {
hours = 12;
} else if (hours >= 12) {
hours = hours - 12;
}
if (hours < 10) {
hours = "0" + hours;
}
if (typeof time !== "string" || !time.includes(":")) {
return "Invalid input: please enter time in HH:MM format"
} if (
isNaN(hours) || hours < 0 || hours > 23
|| isNaN(Number(minutes)) || Number(minutes) < 0
|| Number(minutes) > 59
) {
return "Invalid input: hours must be 0-23 and minutes 0-59";
}
return `${hours}:${minutes} ${period}`;
}
console.log(formatAs12HourClock("19"));
const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
);
const currentOutput2 = formatAs12HourClock("23:00");
const targetOutput2 = "11:00 pm";
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);
console.assert(formatAs12HourClock("00:00") === "12:00 am");
console.assert(formatAs12HourClock("12:00") === "12:00 pm");
console.assert(formatAs12HourClock("13:05") === "01:05 pm");
console.assert(formatAs12HourClock("01:05") === "01:05 am");
console.assert(formatAs12HourClock("23:59") === "11:59 pm");
// after receiving the feedback I wrote the function from scratch and at the end added an other condition to check the input for validation or correct input. if the user puts incorrect input then he will receive a message to correct his input.