Skip to content

Commit 8d5e28c

Browse files
- complete code interpretation for formatTimeDisplay and pad
- Refactor time formatting functions for consistency and clarity - Explain string padding logic and modulo operator calculations for 61 seconds
1 parent 7c3a6c5 commit 8d5e28c

1 file changed

Lines changed: 55 additions & 10 deletions

File tree

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
function pad(num) {
2-
let numString = num.toString();
3-
while (numString.length < 2) {
4-
numString = "0" + numString;
5-
}
6-
return numString;
2+
let numString = num.toString();
3+
while (numString.length < 2) {
4+
numString = "0" + numString;
5+
}
6+
return numString;
77
}
88

99
function formatTimeDisplay(seconds) {
10-
const remainingSeconds = seconds % 60;
11-
const totalMinutes = (seconds - remainingSeconds) / 60;
12-
const remainingMinutes = totalMinutes % 60;
13-
const totalHours = (totalMinutes - remainingMinutes) / 60;
10+
const remainingSeconds = seconds % 60;
11+
const totalMinutes = (seconds - remainingSeconds) / 60;
12+
const remainingMinutes = totalMinutes % 60;
13+
const totalHours = (totalMinutes - remainingMinutes) / 60;
1414

15-
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
15+
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
1616
}
1717

18+
console.log(formatTimeDisplay(61));
19+
1820
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
1921
// to help you answer these questions
2022

@@ -23,16 +25,59 @@ function formatTimeDisplay(seconds) {
2325
// a) When formatTimeDisplay is called how many times will pad be called?
2426
// =============> write your answer here
2527

28+
/* Answer:
29+
The formatTimeDisplay function calls pad three times:
30+
pad(totalHours)
31+
pad(remainingMinutes)
32+
pad(remainingSeconds)
33+
So when formatTimeDisplay is called, pad will be called 3 times.
34+
*/
35+
2636
// Call formatTimeDisplay with an input of 61, now answer the following:
2737

2838
// b) What is the value assigned to num when pad is called for the first time?
2939
// =============> write your answer here
3040

41+
// =============> Answer: The pad function is called for the first time with the argument totalHours, which is 0. So the value assigned to num when pad is called for the first time is 0.
42+
3143
// c) What is the return value of pad when it is called for the first time?
3244
// =============> write your answer here
3345

46+
// =============> Answer: numString = "0" (since 0.toString() is "0"), the while loop checks if "0"length is less than 2, which is true, "0" is padded with a "0" to become "00". So the return value of pad when it is called for the first time is '00'
47+
3448
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
3549
// =============> write your answer here
3650

51+
/* =============> Answer: The formatTimeDisplay function is called with seconds = 61. Inside formatTimeDisplay, these variables are calculated:
52+
remainingSeconds = 61 % 60 = 1
53+
totalMinutes = (61 - 1) / 60 = 1
54+
remainingMinutes = 1 % 60 = 1
55+
totalHours = (1 - 1) / 60 = 0
56+
The pad function is called three times with the values totalHours (0), remainingMinutes (1), and remainingSeconds (1), in that order.
57+
The order of calls to pad is:
58+
pad(0) (for totalHours)
59+
pad(1) (for remainingMinutes)
60+
pad(1) (for remainingSeconds)
61+
The last call to pad is with the argument remainingSeconds, which is 1.
62+
So, the value assigned to num when pad is called for the last time is 1.
63+
*/
64+
3765
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
3866
// =============> write your answer here
67+
68+
/* Answer:
69+
The formatTimeDisplay function is called with seconds = 61.
70+
Inside formatTimeDisplay, these variables are calculated:
71+
remainingSeconds = 61 % 60 = 1
72+
totalMinutes = (61 - 1) / 60 = 1
73+
remainingMinutes = 1 % 60 = 1
74+
totalHours = (1 - 1) / 60 = 0
75+
The pad function is called three times with the values totalHours (0), remainingMinutes (1), and remainingSeconds (1), in that order.
76+
The last call to pad is pad(1):
77+
78+
numString = "1" (since 1.toString() is "1").
79+
The while loop checks if "1".length is less than 2, which is true.
80+
"1" is padded with a "0" to become "01".
81+
The function returns "01".
82+
So, the return value of pad when it's called for the last time is "01".
83+
*/

0 commit comments

Comments
 (0)