Skip to content

Commit e16a1ca

Browse files
committed
Experiment movieLength to a negative value and enhance comments for clarity on variable usage and limitations
1 parent 2ebfefd commit e16a1ca

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const movieLength = 8784; // length of movie in seconds
1+
const movieLength = -9876; // length of movie in seconds
22

33
const remainingSeconds = movieLength % 60;
44
const totalMinutes = (movieLength - remainingSeconds) / 60;
@@ -11,15 +11,41 @@ console.log(result);
1111

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

14+
1415
// a) How many variable declarations are there in this program?
16+
//There are six variable declarations in this program. The lines where variable declarations are made:
17+
//1. Line 1: const movieLength = 8784;
18+
//2. Line 3: const remainingSeconds = movieLength % 60;
19+
//3. Line 4: const totalMinutes = (movieLength - remainingSeconds) / 60;
20+
//4. Line 6: const remainingMinutes = totalMinutes % 60;
21+
//5. Line 7: const totalHours = (totalMinutes - remainingMinutes) / 60;
22+
//6. Line 9: const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
1523

1624
// b) How many function calls are there?
25+
//There are two function calls in this program. The lines where function calls are made are:
26+
//1. Line 9: `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
27+
//2. Line 10: console.log(result);
1728

1829
// c) Using documentation, explain what the expression movieLength % 60 represents
1930
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
2031

32+
//The expression "movieLength % 60" calculates the remainder when the value of "movieLength" is divided by 60.
33+
//In this context, it is used to determine the number of seconds remaining after converting the total movie
34+
//length from seconds into minutes. Since there are 60 seconds in a minute, this operation effectively gives us the
35+
//leftover seconds that do not make up a full minute.
36+
2137
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
38+
//The expression assigned to "totalMinutes" in line 4 calculates the total number of minutes in the movie length.
39+
//It does this by first subtracting the remaining seconds (calculated in line 3) from the total movie length in seconds.
40+
//This gives the total number of seconds that can be fully converted into minutes. Then, it divides that value by 60 to
41+
//convert the total seconds into total minutes. The result is the total number of complete minutes in the movie length,
42+
//excluding any leftover seconds.
2243

2344
// e) What do you think the variable result represents? Can you think of a better name for this variable?
45+
// The variable result represents the formatted time string in the format "hours:minutes:seconds".
46+
// A better name for this variable could be "formattedTime" or "movieDuration"
2447

2548
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
49+
//The code will work for all positive integer values of movieLength, as it correctly calculates the hours, minutes,
50+
//and seconds based on the total length in seconds. But will not work for negative values of movieLength,
51+
//as it will produce negative hours, minutes, and seconds, which does not make sense in the context of a movie length.

0 commit comments

Comments
 (0)