Skip to content

Commit 0067aba

Browse files
committed
2-time-format.js completed
1 parent 9c5fc57 commit 0067aba

File tree

1 file changed

+55
-7
lines changed

1 file changed

+55
-7
lines changed
Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,73 @@
1-
const movieLength = 8784; // length of movie in seconds
1+
//const movieLength = 8784; // length of movie in seconds
22

3-
const remainingSeconds = movieLength % 60;
4-
const totalMinutes = (movieLength - remainingSeconds) / 60;
3+
//const remainingSeconds = movieLength % 60;
4+
//const totalMinutes = (movieLength - remainingSeconds) / 60;
55

6-
const remainingMinutes = totalMinutes % 60;
7-
const totalHours = (totalMinutes - remainingMinutes) / 60;
6+
//const remainingMinutes = totalMinutes % 60;
7+
//const totalHours = (totalMinutes - remainingMinutes) / 60;
88

9-
const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
10-
console.log(result);
9+
//const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
10+
//console.log(result);
1111

1212
// For the piece of code above, read the code and then answer the following questions
1313

1414
// a) How many variable declarations are there in this program?
1515

16+
// There are 6 variable declarations in this program. They are on lines 1, 3, 5, 7, 9 and 11. The variables being
17+
// declared are movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours and result.
18+
19+
20+
1621
// b) How many function calls are there?
1722

23+
// There is 1 function calls in this program. It is on lines 10. The functions is the console.log().
24+
25+
26+
1827
// c) Using documentation, explain what the expression movieLength % 60 represents
1928
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
2029

30+
// % is the remainder operator.
31+
// movieLength % 60 - This gives the remainder when movieLength is divided by 60.
32+
// Since there are 60 seconds in a minute, this tells us how many leftover seconds there are after counting full minutes.
33+
// For example, 8784 % 60 = 24. So there are 24 seconds left after counting the minutes.
34+
35+
36+
2137
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
2238

39+
// movieLength - remainingSecond - This gives the total number of seconds in the movie after removing the leftover seconds.
40+
// Divide by 60 - this converst the seconds into total full minutes. eg. (8784 - 24) = 8760, 8760 / 60 = 146.
41+
42+
43+
2344
// e) What do you think the variable result represents? Can you think of a better name for this variable?
2445

46+
// The variable result is a string that represents the movie length in hours:minutes:seconds
47+
// For example: 2:26:24 (2 hours, 26 minutes, 24 seconds)
48+
// Better name can be movieDurationString.
49+
50+
51+
2552
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
53+
54+
// This code will work for all positive integer values of movieLength. It will correctly calculate the hours, minutes and seconds
55+
// for any length of movie. However, if movieLength is negative or not an integer, the code may not work as intended. For example,
56+
// if movieLength is -100, the calculations will not make sense in the context of a movie length. If movieLength is a decimal, it
57+
// may also cause issues with the calculations.
58+
59+
const movieLength = 65;
60+
61+
const remainingSeconds = movieLength % 60;
62+
const totalMinutes = (movieLength - remainingSeconds) / 60;
63+
64+
const remainingMinutes = totalMinutes % 60;
65+
const totalHours = (totalMinutes - remainingMinutes) / 60;
66+
67+
const formattedHours = String(totalHours).padStart(2, "0");
68+
const formattedMinutes = String(remainingMinutes).padStart(2, "0");
69+
const formattedSeconds = String(remainingSeconds).padStart(2, "0");
70+
71+
const movieDurationString = `${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
72+
73+
console.log(movieDurationString); // 00:01:05

0 commit comments

Comments
 (0)