Skip to content

Commit 53a906d

Browse files
committed
Add explanations for time formatting exercise in time-format.js
1 parent 90591fb commit 53a906d

1 file changed

Lines changed: 43 additions & 5 deletions

File tree

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

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,62 @@ function formatTimeDisplay(seconds) {
1515
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+
//---------------------------------------------------------------------------------------------
24+
2125
// Questions
2226

27+
//---------------------------------------------------------------------------------------------
28+
2329
// a) When formatTimeDisplay is called how many times will pad be called?
24-
// =============> write your answer here
30+
// 3 times.
31+
// pad(totalHours)
32+
// pad(remainingMinutes)
33+
// pad(remainingSeconds)
34+
35+
//---------------------------------------------------------------------------------------------
2536

2637
// Call formatTimeDisplay with an input of 61, now answer the following:
2738

39+
//---------------------------------------------------------------------------------------------
40+
2841
// b) What is the value assigned to num when pad is called for the first time?
29-
// =============> write your answer here
42+
// 0
43+
// remainingSeconds = 61 % 60 = 1
44+
// totalMinutes = (61 - 1) / 60 = 1
45+
// remainingMinutes = 1 % 60 = 1
46+
// totalHours = (1 - 1) / 60 = 0
47+
// First call: pad(totalHours)
48+
// So, num = 0.
49+
50+
//---------------------------------------------------------------------------------------------
3051

3152
// c) What is the return value of pad is called for the first time?
32-
// =============> write your answer here
53+
// "00"
54+
// num = 0
55+
// numString = "0"
56+
// Since numString.length is 1, the while loop runs once.
57+
// "0" + "0" = "00"
58+
// The function returns "00".
59+
60+
//---------------------------------------------------------------------------------------------
3361

3462
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
35-
// =============> write your answer here
63+
// 1
64+
// The last call to pad is pad(remainingSeconds).
65+
// When seconds is 61, remainingSeconds is 61 % 60, which equals 1.
66+
// Therefore, num is assigned the value 1.
67+
68+
//---------------------------------------------------------------------------------------------
3669

3770
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
38-
// =============> write your answer here
71+
// "01"
72+
// The last call is pad(1).
73+
// Inside the function, 1 is converted to the string "1".
74+
// Since its length is less than 2, a leading zero is added,
75+
// making it "01".
76+
// The function then returns "01".

0 commit comments

Comments
 (0)