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
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
19
21
// to help you answer these questions
20
22
@@ -23,16 +25,59 @@ function formatTimeDisplay(seconds) {
23
25
// a) When formatTimeDisplay is called how many times will pad be called?
24
26
// =============> write your answer here
25
27
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
+
26
36
// Call formatTimeDisplay with an input of 61, now answer the following:
27
37
28
38
// b) What is the value assigned to num when pad is called for the first time?
29
39
// =============> write your answer here
30
40
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
+
31
43
// c) What is the return value of pad when it is called for the first time?
32
44
// =============> write your answer here
33
45
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
+
34
48
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
35
49
// =============> write your answer here
36
50
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
+
37
65
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
38
66
// =============> 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".
0 commit comments