-
-
Notifications
You must be signed in to change notification settings - Fork 378
Expand file tree
/
Copy pathtime-format.js
More file actions
47 lines (37 loc) · 1.63 KB
/
Copy pathtime-format.js
File metadata and controls
47 lines (37 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
function pad(num) {
let numString = num.toString();
while (numString.length < 2) {
numString = "0" + numString;
}
return numString;
}
function formatTimeDisplay(seconds) {
const remainingSeconds = seconds % 60;
const totalMinutes = (seconds - remainingSeconds) / 60;
const remainingMinutes = totalMinutes % 60;
const totalHours = (totalMinutes - remainingMinutes) / 60;
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}
// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
// to help you answer these questions
// Questions
// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// 3
// Call formatTimeDisplay with an input of 61, now answer the following:
// b) What is the value assigned to num when pad is called for the first time?
// =============> write your answer here
// 0
// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// 00
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
// =============> write your answer here
// 1
// When pad is called for the last time it is here pad(remainingSeconds)
// remaining seconds = seconds % 60 = 61 % 60 = 1
// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
// =============> write your answer here
// 01
// when pad is called it takes the num, in this case 1, and pads it out to two digits by adding zeros at the front
// so 1 becomes 01