Skip to content

Commit 4aa5b32

Browse files
committed
Complete time-format interpret exercise with answers
1 parent 35cbc03 commit 4aa5b32

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

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

Lines changed: 19 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 = 9325; // length of movie in seconds
22

33
const remainingSeconds = movieLength % 60;
44
const totalMinutes = (movieLength - remainingSeconds) / 60;
@@ -12,14 +12,32 @@ console.log(result);
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?
15+
// Answer: 6 variable declarations.
1516

1617
// b) How many function calls are there?
18+
// Answer: 1 function call.
1719

1820
// c) Using documentation, explain what the expression movieLength % 60 represents
1921
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
22+
// Answer: The % operator is the remainder (modulo) operator.
23+
// movieLength % 60 gives the remainder after dividing the total seconds by 60.
24+
// That remainder is the number of seconds left after removing complete minutes.
25+
// Example: 8784 % 60 === 24.
2026

2127
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
28+
// Answer: (movieLength - remainingSeconds) / 60
29+
// First it subtracts the leftover seconds so the value divides evenly by 60,
30+
// then it divides by 60 to get the total number of whole minutes in the movie.
2231

2332
// e) What do you think the variable result represents? Can you think of a better name for this variable?
33+
// Answer: result stores the movie length formatted as hours:minutes:seconds
34+
// (for this value: "2:26:24").
35+
// A clearer name would be formattedTime.
2436

2537
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
38+
// Answer: It works for non-negative whole numbers of seconds and correctly
39+
// splits time into hours, minutes, and seconds.
40+
// Limitations:
41+
// - fractional values can produce messy decimals
42+
// - negative values do not make sense for a movie length.
43+
// Note: I have changed the movieLength from "8784" to "9325" for testing.

0 commit comments

Comments
 (0)