You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Sprint-2/5-stretch-extend/format-time.js
+33-1Lines changed: 33 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@
3
3
// 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.
4
4
// --- > 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.
5
5
6
-
functionformatAs12HourClock(time){
6
+
/*function formatAs12HourClock(time) {
7
7
const hours = Number(time.slice(0, 2));
8
8
const minutes = time.slice(2);
9
9
@@ -27,7 +27,37 @@ function formatAs12HourClock(time) {
27
27
} else {
28
28
return `${formattedHours}${minutes} am`;
29
29
}
30
+
} */
31
+
32
+
functionformatAs12HourClock(time){
33
+
letparts=time.split(":");
34
+
lethours=Number(parts[0]);
35
+
letminutes=parts[1];
36
+
37
+
letperiod="am";
38
+
if(hours>=12){
39
+
period="pm";
40
+
}
41
+
if(hours===0){
42
+
hours=12;
43
+
}elseif(hours>=12){
44
+
hours=hours-12;
45
+
}
46
+
if(hours<10){
47
+
hours="0"+hours;
48
+
}
49
+
if(typeoftime!=="string"||!time.includes(":")){
50
+
return"Invalid input: please enter time in HH:MM format"
51
+
}if(
52
+
isNaN(hours)||hours<0||hours>23
53
+
||isNaN(Number(minutes))||Number(minutes)<0
54
+
||Number(minutes)>59
55
+
){
56
+
return"Invalid input: hours must be 0-23 and minutes 0-59";
// 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.
0 commit comments