|
1 | | -const movieLength = 8784; // length of movie in seconds |
| 1 | +//const movieLength = 8784; // length of movie in seconds |
2 | 2 |
|
3 | | -const remainingSeconds = movieLength % 60; |
4 | | -const totalMinutes = (movieLength - remainingSeconds) / 60; |
| 3 | +//const remainingSeconds = movieLength % 60; |
| 4 | +//const totalMinutes = (movieLength - remainingSeconds) / 60; |
5 | 5 |
|
6 | | -const remainingMinutes = totalMinutes % 60; |
7 | | -const totalHours = (totalMinutes - remainingMinutes) / 60; |
| 6 | +//const remainingMinutes = totalMinutes % 60; |
| 7 | +//const totalHours = (totalMinutes - remainingMinutes) / 60; |
8 | 8 |
|
9 | | -const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; |
10 | | -console.log(result); |
| 9 | +//const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; |
| 10 | +//console.log(result); |
11 | 11 |
|
12 | 12 | // For the piece of code above, read the code and then answer the following questions |
13 | 13 |
|
14 | 14 | // a) How many variable declarations are there in this program? |
15 | 15 |
|
| 16 | +// There are 6 variable declarations in this program. They are on lines 1, 3, 5, 7, 9 and 11. The variables being |
| 17 | +// declared are movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours and result. |
| 18 | + |
| 19 | + |
| 20 | + |
16 | 21 | // b) How many function calls are there? |
17 | 22 |
|
| 23 | +// There is 1 function calls in this program. It is on lines 10. The functions is the console.log(). |
| 24 | + |
| 25 | + |
| 26 | + |
18 | 27 | // c) Using documentation, explain what the expression movieLength % 60 represents |
19 | 28 | // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators |
20 | 29 |
|
| 30 | +// % is the remainder operator. |
| 31 | +// movieLength % 60 - This gives the remainder when movieLength is divided by 60. |
| 32 | +// Since there are 60 seconds in a minute, this tells us how many leftover seconds there are after counting full minutes. |
| 33 | +// For example, 8784 % 60 = 24. So there are 24 seconds left after counting the minutes. |
| 34 | + |
| 35 | + |
| 36 | + |
21 | 37 | // d) Interpret line 4, what does the expression assigned to totalMinutes mean? |
22 | 38 |
|
| 39 | +// movieLength - remainingSecond - This gives the total number of seconds in the movie after removing the leftover seconds. |
| 40 | +// Divide by 60 - this converst the seconds into total full minutes. eg. (8784 - 24) = 8760, 8760 / 60 = 146. |
| 41 | + |
| 42 | + |
| 43 | + |
23 | 44 | // e) What do you think the variable result represents? Can you think of a better name for this variable? |
24 | 45 |
|
| 46 | +// The variable result is a string that represents the movie length in hours:minutes:seconds |
| 47 | +// For example: 2:26:24 (2 hours, 26 minutes, 24 seconds) |
| 48 | +// Better name can be movieDurationString. |
| 49 | + |
| 50 | + |
| 51 | + |
25 | 52 | // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer |
| 53 | + |
| 54 | +// This code will work for all positive integer values of movieLength. It will correctly calculate the hours, minutes and seconds |
| 55 | +// for any length of movie. However, if movieLength is negative or not an integer, the code may not work as intended. For example, |
| 56 | +// if movieLength is -100, the calculations will not make sense in the context of a movie length. If movieLength is a decimal, it |
| 57 | +// may also cause issues with the calculations. |
| 58 | + |
| 59 | +const movieLength = 65; |
| 60 | + |
| 61 | +const remainingSeconds = movieLength % 60; |
| 62 | +const totalMinutes = (movieLength - remainingSeconds) / 60; |
| 63 | + |
| 64 | +const remainingMinutes = totalMinutes % 60; |
| 65 | +const totalHours = (totalMinutes - remainingMinutes) / 60; |
| 66 | + |
| 67 | +const formattedHours = String(totalHours).padStart(2, "0"); |
| 68 | +const formattedMinutes = String(remainingMinutes).padStart(2, "0"); |
| 69 | +const formattedSeconds = String(remainingSeconds).padStart(2, "0"); |
| 70 | + |
| 71 | +const movieDurationString = `${formattedHours}:${formattedMinutes}:${formattedSeconds}`; |
| 72 | + |
| 73 | +console.log(movieDurationString); // 00:01:05 |
0 commit comments