|
| 1 | +console.log("entered data") |
1 | 2 | function pad(num) { |
| 3 | + console.log("pad has been called: Num is", num); |
2 | 4 | let numString = num.toString(); |
3 | 5 | while (numString.length < 2) { |
4 | 6 | numString = "0" + numString; |
5 | 7 | } |
| 8 | + console.log("numString: ", numString); |
6 | 9 | return numString; |
7 | 10 | } |
8 | 11 |
|
9 | 12 | function formatTimeDisplay(seconds) { |
| 13 | + console.log("entered formatTimeDisplay"); |
10 | 14 | const remainingSeconds = seconds % 60; |
| 15 | + console.log("remainingSeconds: ", remainingSeconds); |
11 | 16 | const totalMinutes = (seconds - remainingSeconds) / 60; |
| 17 | + console.log("totalMinutes: ", totalMinutes); |
12 | 18 | const remainingMinutes = totalMinutes % 60; |
| 19 | + console.log("remainingMinutes: ", remainingMinutes); |
13 | 20 | const totalHours = (totalMinutes - remainingMinutes) / 60; |
| 21 | + console.log("totalHours: ", totalHours); |
| 22 | + |
14 | 23 |
|
15 | 24 | return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; |
16 | 25 | } |
17 | | - |
| 26 | +console.log(formatTimeDisplay(61)) |
18 | 27 | // You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit |
19 | 28 | // to help you answer these questions |
20 | 29 |
|
21 | 30 | // Questions |
22 | 31 |
|
23 | 32 | // a) When formatTimeDisplay is called how many times will pad be called? |
24 | 33 | // =============> write your answer here |
| 34 | +// Pad is being called 3 times |
25 | 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 |
| 40 | +// 0 |
30 | 41 |
|
31 | 42 | // c) What is the return value of pad when it is called for the first time? |
32 | 43 | // =============> write your answer here |
| 44 | +// 00 |
33 | 45 |
|
34 | 46 | // d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer |
35 | 47 | // =============> write your answer here |
| 48 | +// 1, The value given is the remainder after being used in remainingMinutes section of the code |
36 | 49 |
|
37 | 50 | // e) What is the return value of pad when it is called for the last time in this program? Explain your answer |
38 | 51 | // =============> write your answer here |
| 52 | +// 01, after going through pad it ensures that there is two 00 if there is no value left, with just 1 it adds one 0 to the front of 1. |
0 commit comments