Skip to content

Commit 8fb5a39

Browse files
committed
answered all the question and fixed the code.
1 parent e897f8b commit 8fb5a39

File tree

2 files changed

+78
-7
lines changed

2 files changed

+78
-7
lines changed

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

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,73 @@ function formatTimeDisplay(seconds) {
1919
// a) When formatTimeDisplay is called how many times will pad be called?
2020
// =============> write your answer here
2121

22-
// Call formatTimeDisplay with an input of 61, now answer the following:
22+
Answer: 3
23+
24+
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
25+
26+
pad is called three times: for totalHours, remainingMinutes, and remainingSeconds.
27+
28+
So no matter what input we give to formatTimeDisplay, pad will always be called 3 times.
2329

30+
// Call formatTimeDisplay with an input of 61, now answer the following:
2431
// b) What is the value assigned to num when pad is called for the first time?
2532
// =============> write your answer here
2633

34+
Answer;
35+
36+
The first call is pad(totalHours).
37+
38+
const totalHours = (totalMinutes - remainingMinutes) / 60;
39+
40+
The value assigned to num when pad is called for the first time is 0.
41+
Consequently, pad(0) will return '00', which ensures that the hour is formatted correctly in the final output string
42+
43+
2744
// c) What is the return value of pad is called for the first time?
2845
// =============> write your answer here
2946

47+
Answer; 1
48+
49+
return num.toString().padStart(2, "0");
50+
51+
num = 1
52+
53+
num.toString() = "1"
54+
55+
"1".padStart(2, "0") = "01"
56+
57+
If you call pad(0), the return value will be '00'.
58+
59+
If you call pad(1), the return value will be '01'.
60+
61+
If you call pad(5), the return value will be '05'.
62+
63+
If you call pad(10), the return value will be '10'
64+
65+
66+
3067
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
3168
// =============> write your answer here
69+
Answer; 1
70+
71+
The last call is pad(remainingSeconds).
72+
73+
When you call format_time_display(61):
74+
75+
The calculations result in:
76+
77+
total_hours = 0
78+
remaining_minutes = 1
79+
remaining_seconds = 1
80+
3281

3382
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
3483
// =============> write your answer here
84+
Answwer; 01
85+
86+
87+
Third Call: pad(remaining_seconds) (where remaining_seconds = 1)
88+
89+
Value of num for the last call: 1
90+
91+
Return value: '01'

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,36 @@
33
// 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.
44

55
function formatAs12HourClock(time) {
6-
const hours = Number(time.slice(0, 2));
6+
// Expect time in "HH:MM" format
7+
let hours = Number(time.slice(0, 2));
8+
let mins = time.slice(3);
9+
10+
if (hours === 0) {
11+
return `12:${mins} am`;
12+
}
13+
14+
if (hours === 12) {
15+
return `12:${mins} pm`;
16+
}
17+
718
if (hours > 12) {
8-
return `${hours - 12}:00 pm`;
19+
let newHr = hours - 12;
20+
// pad single digits like 1:00 -> 01:00
21+
return `${String(newHr).padStart(2, "0")}:${mins} pm`;
922
}
23+
1024
return `${time} am`;
1125
}
1226

13-
const currentOutput = formatAs12HourClock("08:00");
14-
const targetOutput = "08:00 am";
27+
let currentOutput = formatAs12HourClock("08:00");
28+
let targetOutput = "08:00 am";
1529
console.assert(
1630
currentOutput === targetOutput,
1731
`current output: ${currentOutput}, target output: ${targetOutput}`
1832
);
1933

20-
const currentOutput2 = formatAs12HourClock("23:00");
21-
const targetOutput2 = "11:00 pm";
34+
let currentOutput2 = formatAs12HourClock("23:00");
35+
let targetOutput2 = "11:00 pm";
2236
console.assert(
2337
currentOutput2 === targetOutput2,
2438
`current output: ${currentOutput2}, target output: ${targetOutput2}`

0 commit comments

Comments
 (0)