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
54 lines (42 loc) · 2.08 KB
/
time-format.js
File metadata and controls
54 lines (42 loc) · 2.08 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
48
49
50
51
52
53
54
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?
// Here The pad function is called three times.
// 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?
// When formatTimeDisplay is called with an input of 143, the value will be assigned to the parameter num when pad is called for the first time is 2.
// c) What is the return value of pad when it is called for the first time?
//The return value of pad when it is called for the first time is 02
// d) What is the value assigned to the parameter num when pad
// is called for the last time in this program? Explain your answer
//The value assigned to the parameter num when pad is called for the last time in this program is 00.
// because the remaining seconds is the remainder of the seconds after dividing by 60
// e) What is the return value when pad is called
// for the last time in this program? Explain your answer
//The return value when pad is called for the last time in this program is 23.
//because the assigned value will return the num value itself
// f) Research an alternative way of padding the numbers in this code.
// Look up the string functions on mdn
//we can use padStart(2, '0') to pad the numbers to a length of two characters with the padding character '0'. like this:
//function pad(num) {
//return num.toString().padStart(2, '0');
//}