Skip to content

Commit 2e95c35

Browse files
committed
Update 2-time-format.js
Code amended to allow shorter times.
1 parent 4a41676 commit 2e95c35

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

Sprint-1/3-mandatory-interpret/2-time-format.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
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

@@ -50,4 +50,20 @@ console.log(result);
5050

5151
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
5252

53-
// This code will work for all positive integer values of movieLength. It will correctly calculate the hours, minutes and seconds for any length of movie. However, if movieLength is negative or not an integer, the code may not work as intended. For example, if movieLength is -100, the calculations will not make sense in the context of a movie length. If movieLength is a decimal, it may also cause issues with the calculations.
53+
// This code will work for all positive integer values of movieLength. It will correctly calculate the hours, minutes and seconds for any length of movie. However, if movieLength is negative or not an integer, the code may not work as intended. For example, if movieLength is -100, the calculations will not make sense in the context of a movie length. If movieLength is a decimal, it may also cause issues with the calculations.
54+
55+
const movieLength = 65;
56+
57+
const remainingSeconds = movieLength % 60;
58+
const totalMinutes = (movieLength - remainingSeconds) / 60;
59+
60+
const remainingMinutes = totalMinutes % 60;
61+
const totalHours = (totalMinutes - remainingMinutes) / 60;
62+
63+
const formattedHours = String(totalHours).padStart(2, "0");
64+
const formattedMinutes = String(remainingMinutes).padStart(2, "0");
65+
const formattedSeconds = String(remainingSeconds).padStart(2, "0");
66+
67+
const movieDurationString = `${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
68+
69+
console.log(movieDurationString); // 00:01:05

0 commit comments

Comments
 (0)