Skip to content

Commit 430be1d

Browse files
debug2604debug2604
authored andcommitted
Answered 2-time-format.js
1 parent c504786 commit 430be1d

1 file changed

Lines changed: 33 additions & 25 deletions

File tree

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
1-
const movieLength = 8784; // length of movie in seconds
2-
3-
const remainingSeconds = movieLength % 60;
4-
const totalMinutes = (movieLength - remainingSeconds) / 60;
5-
6-
const remainingMinutes = totalMinutes % 60;
7-
const totalHours = (totalMinutes - remainingMinutes) / 60;
8-
9-
const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
10-
console.log(result);
11-
12-
// For the piece of code above, read the code and then answer the following questions
13-
14-
// a) How many variable declarations are there in this program?
15-
16-
// b) How many function calls are there?
17-
18-
// c) Using documentation, explain what the expression movieLength % 60 represents
19-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
20-
21-
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
22-
23-
// e) What do you think the variable result represents? Can you think of a better name for this variable?
24-
25-
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
1+
const movieLength = 8784; // length of movie in seconds
2+
3+
const remainingSeconds = movieLength % 60;
4+
const totalMinutes = (movieLength - remainingSeconds) / 60;
5+
6+
const remainingMinutes = totalMinutes % 60;
7+
const totalHours = (totalMinutes - remainingMinutes) / 60;
8+
9+
const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
10+
console.log(result);
11+
12+
// For the piece of code above, read the code and then answer the following questions
13+
14+
// a) How many variable declarations are there in this program?
15+
// There are six variable declarations in the program, namely :
16+
// movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, result
17+
18+
// b) How many function calls are there?
19+
// One function call console.log() in code above.
20+
// The others are declared variables.
21+
22+
// c) Using documentation, explain what the expression movieLength % 60 represents
23+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
24+
// % is remainer operator. This operator returns the remainder after left operand is divided by 60.
25+
26+
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
27+
// The expression means first exclude odd seconds, then convert the movie length in number of complete minutes.
28+
29+
// e) What do you think the variable result represents? Can you think of a better name for this variable?
30+
// The variable result represents length of movie in H:M:S format. A better variable name can be movieLength_HMS
31+
32+
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
33+
// A value of movieLength 3661 will return a result of 1:1:1 where the place value for second does not conforms with leading zero time format.

0 commit comments

Comments
 (0)