|
1 | | -function pad(num) { |
2 | | - return num.toString().padStart(2, "0"); |
| 1 | + return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; |
3 | 2 | } |
4 | 3 |
|
5 | | -function formatTimeDisplay(seconds) { |
6 | | - const remainingSeconds = seconds % 60; |
7 | | - const totalMinutes = (seconds - remainingSeconds) / 60; |
8 | | - const remainingMinutes = totalMinutes % 60; |
9 | | - const totalHours = (totalMinutes - remainingMinutes) / 60; |
| 4 | +console.log(formatTimeDisplay(61)); |
| 5 | +console.log(formatTimeDisplay(6671)); |
| 6 | +console.log(formatTimeDisplay(832)); |
10 | 7 |
|
11 | | - return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; |
12 | | -} |
13 | 8 |
|
14 | 9 | // You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit |
15 | 10 | // to help you answer these questions |
16 | 11 |
|
17 | 12 | // Questions |
18 | 13 |
|
19 | 14 | // a) When formatTimeDisplay is called how many times will pad be called? |
20 | | -// =============> write your answer here |
| 15 | +// =============> write your answer here: pad will be called 3 times. |
21 | 16 |
|
22 | 17 | // Call formatTimeDisplay with an input of 61, now answer the following: |
23 | 18 |
|
24 | 19 | // b) What is the value assigned to num when pad is called for the first time? |
25 | | -// =============> write your answer here |
| 20 | +// =============> write your answer here: The value is 0. |
26 | 21 |
|
27 | 22 | // c) What is the return value of pad is called for the first time? |
28 | | -// =============> write your answer here |
| 23 | +// =============> write your answer here: The return value is '00'. |
29 | 24 |
|
30 | 25 | // d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer |
31 | | -// =============> write your answer here |
| 26 | +// =============> write your answer here: The value is 1 because 61 % 60 leaves 1 second remaining. |
32 | 27 |
|
33 | 28 | // e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer |
34 | | -// =============> write your answer here |
| 29 | +// =============> write your answer here: The return value is '01' because return num.toString().padstart(2, '0') formats 1 into '01'. |
0 commit comments