This repository was archived by the owner on Apr 18, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathtime-format.js
More file actions
44 lines (32 loc) · 1.46 KB
/
time-format.js
File metadata and controls
44 lines (32 loc) · 1.46 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
function pad(num) {
if (num < 10) {
return `0${num}`;
}
return num;
}
function formatTimeDisplay(seconds) {
const remainingSeconds = seconds % 60;
const totalMinutes = (seconds - remainingSeconds) / 60;
const remainingMinutes = totalMinutes % 60;
const totalHours = (totalMinutes - remainingMinutes) / 60;
const remainingHours = totalHours % 24;
return `${pad(remainingHours)}:${pad(remainingMinutes)}:${pad(
remainingSeconds
)}`;
}
console.log(formatTimeDisplay(143));
// You can 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?
// =>The pad function will be called three times within the formatTimeDisplay function: once for hours, once for minutes, and once for seconds.
// Call formatTimeDisplay with an input of 143, now answer the following:
// b) What value is assigned to the parameter num when pad is called for the first time?
// c) What is the return value of pad when it is called for the first time?
// d) What is the value assigned to the parameter num when pad
// is called for the last time in this program? Explain your answer
// e) What is the return value when pad is called
// for the last time in this program? Explain your answer
// f) Research an alternative way of padding the numbers in this code.
// Look up the string functions on mdn