You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Subtracts the leftover seconds (that couldn’t form a full minute).
51
+
52
+
// Divides the remaining seconds by 60.
53
+
// Meaning:
54
+
// Converts the total movie length into minutes, ignoring any incomplete minute.
55
+
// So it gives the total number of full minutes in the movie.
56
+
23
57
// e) What do you think the variable result represents? Can you think of a better name for this variable?
24
58
59
+
// const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
60
+
61
+
// This combines hours, minutes, and seconds into a single string formatted like H:M:S.
62
+
// Example output: "2:26:24"
63
+
// Meaning: It represents the formatted time duration of the movie.
64
+
// Better variable names:
65
+
// formattedDuration
66
+
// timeString
67
+
// movieTime
68
+
// durationInHMS
69
+
25
70
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
71
+
72
+
// It will work correctly for most positive integers, since it’s just converting seconds into hours, minutes, and seconds using division and remainders.
73
+
// However, there are some edge cases:
74
+
// If movieLength = 0:
75
+
// Output: 0:0:0 ✅ Works fine.
76
+
// If movieLength < 60 (less than a minute):
77
+
// Works fine (e.g., 45 → 0:0:45).
78
+
// If movieLength is not an integer (e.g., 87.5):
79
+
// Still works, but decimals might appear in remainingSeconds.
80
+
// If movieLength is negative:
81
+
// The logic breaks — you’d get negative time components ❌.
// What it does: ensures the numeric string has at least 3 characters by adding leading "0" characters if necessary.
48
+
// Why: the subsequent logic expects at least three digits so we can safely slice off "pounds" (all but the last two digits) and "pence" (last two digits). Padding handles small values like "5p" → ensures correct indexing.
49
+
// Example: "399".padStart(3, "0") → "399" (no change). If input had been "5", .padStart(3,"0") → "005".
50
+
// Result stored: paddedPenceNumberString === "399"
// What it does: takes the substring from index 0 up to (but not including) the last two characters. Those leading characters represent whole pounds.
59
+
// Why: British currency: last two digits are pence, the digits before them are pounds. This extracts the pounds portion.
60
+
// Example: "399".substring(0, 3 - 2) → "3". So pounds === "3", meaning £3.
61
+
// Note: Because of padStart(3, "0"), this always returns at least one character (for values < 100 it returns "0" or more).
62
+
63
+
// const pence = paddedPenceNumberString
64
+
// .substring(paddedPenceNumberString.length - 2)
65
+
// .padEnd(2, "0");
66
+
67
+
68
+
// What it does (first part): .substring(length - 2) returns the final two characters (the pence digits).
69
+
// What it does (second part): .padEnd(2, "0") ensures the result has at least two characters by adding trailing zeros if needed.
70
+
// Why: extracting the two pence digits is necessary to format £x.yy. The padEnd is defensive: if the substring somehow produced a single character (it shouldn't after the earlier padStart, but this makes the code more robust), it will ensure two digits (e.g., "5" → "50").
71
+
// Example: "399".substring(1) → "99". .padEnd(2,"0") → still "99". So pence === "99".
72
+
73
+
// console.log(`£${pounds}.${pence}`);
74
+
75
+
76
+
// What it does: prints a formatted pounds-and-pence string to the console, using template interpolation.
77
+
// Why: final user-facing representation of the price.
78
+
// Example output: £3.99
79
+
80
+
// Quick summary of the data flow (with the example "399p")
81
+
82
+
// penceString → "399p"
83
+
84
+
// remove trailing p → "399" (penceStringWithoutTrailingP)
85
+
86
+
// pad to at least 3 chars → "399" (paddedPenceNumberString)
87
+
88
+
// pounds = all but last two chars → "3"
89
+
90
+
// pence = last two chars → "99"
91
+
92
+
// printed string → £3.99
93
+
94
+
// Why some steps (like padStart / padEnd) are needed
95
+
96
+
// padStart(3, "0") ensures inputs shorter than 3 digits (e.g. "5p" or "45p") still produce correct pound/pence splitting:
// padEnd(2, "0") is defensive to guarantee two pence digits (useful if some earlier step produced one digit).
103
+
104
+
// Edge cases & small improvements
105
+
106
+
// If the input lacks the trailing "p" or contains non-digits ("£3.99", "abc") the code will produce incorrect results or unexpected output. Consider validating the string first (e.g., /^\d+p$/).
107
+
108
+
// For clarity and robustness you could parse the number then compute pounds/pence numerically:
// This avoids fiddly substring indices and better expresses intent.
116
+
117
+
// padEnd(2, "0") after slicing the last two characters is usually unnecessary if padStart(3,"0") is present — but leaving it is harmless and defensive.
0 commit comments