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
21
23
// Questions
22
24
23
25
// a) When formatTimeDisplay is called how many times will pad be called?
24
26
// =============> write your answer here
27
+
//> pad() was called 3 times---> pad(totalHours)---> pad(remainingMinutes)---> pad(remainingSeconds)
25
28
26
29
// Call formatTimeDisplay with an input of 61, now answer the following:
30
+
// formatTimeDisplay(61);
27
31
28
32
// b) What is the value assigned to num when pad is called for the first time?
29
33
// =============> write your answer here
34
+
// pad(totalHours)--> totalHours= 0 --> Therefore num = 0
30
35
31
36
// c) What is the return value of pad when it is called for the first time?
32
37
// =============> write your answer here
38
+
//the first pad() call returns the answer 00, this is because the value is 0, and functions adds a 0 to the beginning of the character to make it two characters long.
33
39
34
40
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
35
41
// =============> write your answer here
42
+
// 3 pad calls---> 1)-> pad(0), 2)-> pad(1), 3)-> pad(1)---> this shows the last call pad(remainingSeconds) = remainingSeconds= 1. when we call formatTimeDisplay(61); indicates that there are 61 seconds, after taking away 60 seconds which makes it 1 minute and the remainder is 1 second.
36
43
37
44
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
38
45
// =============> write your answer here
46
+
// when calling formatTimeDisplay(61); we already figured out the last pad() call is pad (remainingSeconds) and know that remainingSeconds is 1. Inside pad the number changes into a string "1" and the function then calculates and checks the length of the string but we wanted 2 characters/digits at least. Hence why the function adds 0 to the beginning which then gives us "01".
0 commit comments