Skip to content

Commit be792fb

Browse files
authored
solving by identifying edge cases
1 parent 3d9c508 commit be792fb

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Sprint-2/4-mandatory-interpret/time-format.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ function formatTimeDisplay(seconds) {
1010

1111
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
1212
}
13+
console.log(formatTimeDisplay(61));
1314

1415
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
1516
// to help you answer these questions
@@ -18,17 +19,22 @@ function formatTimeDisplay(seconds) {
1819

1920
// a) When formatTimeDisplay is called how many times will pad be called?
2021
// =============> write your answer here
22+
/* pad will be called 3 times. one for totalHours, one for remainingMinutes and one for remainingSeconds.*/
2123

2224
// Call formatTimeDisplay with an input of 61, now answer the following:
2325

2426
// b) What is the value assigned to num when pad is called for the first time?
2527
// =============> write your answer here
28+
/* when I run the code the result is 00:01:01 there for the value assigned to num is "0".
2629
2730
// c) What is the return value of pad is called for the first time?
2831
// =============> write your answer here
32+
/* the return value is "00". this comes 0 changed to string and then padstart(2,0) changes it to "00" */
2933

3034
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
3135
// =============> write your answer here
36+
/* The value assigned to num when the pad is called for the last time is "1". */
3237

3338
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
3439
// =============> write your answer here
40+
/* The return value is 1. The code 1.toString() changes the num 1 to string "1" then the code .padStart(2, "0"); changes "1" to "01"

Sprint-2/5-stretch-extend/format-time.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,17 @@
44

55
function formatAs12HourClock(time) {
66
const hours = Number(time.slice(0, 2));
7+
if (hours===0) {
8+
return`12:00 am`
9+
}
10+
if (hours===12) {
11+
return `12:00 pm`
12+
}
713
if (hours > 12) {
814
return `${hours - 12}:00 pm`;
915
}
10-
return `${time} am`;
16+
if (hours > 0 && hours < 12)
17+
return `${String(hours).padStart(2, "0")}:00 am`;
1118
}
1219

1320
const currentOutput = formatAs12HourClock("08:00");
@@ -22,4 +29,17 @@ const targetOutput2 = "11:00 pm";
2229
console.assert(
2330
currentOutput2 === targetOutput2,
2431
`current output: ${currentOutput2}, target output: ${targetOutput2}`
32+
33+
);
34+
const currentOutput3 = formatAs12HourClock("00:00");
35+
const targetOutput3 = "12:00 am";
36+
console.assert(
37+
currentOutput3 === targetOutput3,
38+
`current output: ${currentOutput2}, target output: ${targetOutput2}`
39+
);
40+
const currentOutput4 = formatAs12HourClock("12:00");
41+
const targetOutput4 = "12:00 pm";
42+
console.assert(
43+
currentOutput4 === targetOutput4,
44+
`current output: ${currentOutput2}, target output: ${targetOutput2}`
2545
);

0 commit comments

Comments
 (0)