@@ -12,14 +12,59 @@ 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+ /*there are six variable declarations in this program:
16+ 1. movieLength
17+ 2. remainingSeconds
18+ 3. totalMinutes
19+ 4. remainingMinutes
20+ 5. totalHours
21+ 6. result
22+ */
1523
1624// b) How many function calls are there?
1725
26+ /*There are no function calls in this code. Everything here is using operators and expressions, not calling functions.*/
27+
1828// c) Using documentation, explain what the expression movieLength % 60 represents
1929// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
2030
31+ /*The % operator in JavaScript is the remainder operator. It divides the left-hand number by the right-hand number and returns the remainder.
32+
33+ So in this case:
34+
35+ movieLength % 60
36+
37+ divides movieLength (8784) by 60 and gives the remaining seconds, which is 24.*/
38+
39+
2140// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
2241
42+ /*The expression
43+
44+ totalMinutes = (movieLength - remainingSeconds) / 60;
45+
46+ subtracts the leftover seconds from the total seconds so that we get a whole number of minutes.
47+ Then it divides by 60 to convert seconds into minutes without any decimal part.*/
48+
49+
2350// e) What do you think the variable result represents? Can you think of a better name for this variable?
51+ /*The variable result represents the length of the movie in the format hours:minutes:seconds.
52+
53+ A better name could be something like:
54+
55+ movieDurationFormatted
56+
57+ formattedDuration
58+
59+ movieLengthHMS (HMS = hours, minutes, seconds)*/
60+
61+ // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer,
62+ /*Yes, it works for all non-negative values:
63+
64+ If the movie is less than an hour, totalHours is 0, so we get 0:MM:SS.
65+
66+ If the movie is less than a minute, both totalHours and remainingMinutes are 0, so we get 0:0:SS.
67+
68+ For larger movies, it correctly calculates hours, minutes, and seconds.
2469
25- // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
70+ The only exception would be negative values, which aren’t realistic for a movie length.*/
0 commit comments