Skip to content

Commit a36e485

Browse files
committed
commit 2-time-format.js and 3-to-pounds.js
1 parent 7e75245 commit a36e485

File tree

2 files changed

+161
-5
lines changed

2 files changed

+161
-5
lines changed

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,79 @@ 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+
// Each const introduces a new variable.
16+
//
17+
// movieLength
18+
// remainingSeconds
19+
// totalMinutes
20+
// remainingMinutes
21+
// totalHours
22+
// result
23+
// Total variable declarations: 6
1524

1625
// b) How many function calls are there?
1726

27+
// The only function call is:
28+
29+
// Total function calls: 1
30+
// console.log(result);
31+
1832
// c) Using documentation, explain what the expression movieLength % 60 represents
1933
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
2034

35+
// According to the MDN documentation on the remainder (%) operator
36+
// The remainder operator returns the remainder left over when one operand is divided by a second operand.
37+
// So:
38+
// movieLength % 60
39+
// means “the remainder when movieLength is divided by 60.”
40+
// Interpretation:
41+
// It gives you the remaining seconds that don’t make up a full minute.
42+
// For example, if the movie length were 125 seconds:
43+
// 125 % 60 = 5, so there are 5 seconds left over after 2 full minutes.
44+
// Purpose: Extract the leftover seconds after converting total seconds into minutes.
45+
2146
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
2247

48+
// const totalMinutes = (movieLength - remainingSeconds) / 60;
49+
// This line:
50+
// 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+
2357
// e) What do you think the variable result represents? Can you think of a better name for this variable?
2458

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+
2570
// 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 ❌.
82+
// Formatting issue:
83+
84+
// The output isn’t padded.
85+
// Example: 2 hours, 5 minutes, 9 seconds → "2:5:9" instead of "02:05:09".
86+
// (Can fix that with .padStart(2, '0') on each component.)
87+
88+
// Summary:
89+
// It works for positive whole numbers, but for negative or non-integer inputs, or if you need formatted time, adjustments are needed.
90+

Sprint-1/3-mandatory-interpret/3-to-pounds.js

Lines changed: 96 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
const penceString = "399p";
1+
let penceString = "399p";
22

3-
const penceStringWithoutTrailingP = penceString.substring(
3+
let penceStringWithoutTrailingP = penceString.substring(
44
0,
55
penceString.length - 1
66
);
77

8-
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
9-
const pounds = paddedPenceNumberString.substring(
8+
let paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
9+
let pounds = paddedPenceNumberString.substring(
1010
0,
1111
paddedPenceNumberString.length - 2
1212
);
1313

14-
const pence = paddedPenceNumberString
14+
let pence = paddedPenceNumberString
1515
.substring(paddedPenceNumberString.length - 2)
1616
.padEnd(2, "0");
1717

@@ -25,3 +25,94 @@ console.log(`£${pounds}.${pence}`);
2525

2626
// To begin, we can start with
2727
// 1. const penceString = "399p": initialises a string variable with the value "399p"
28+
29+
// const penceString = "399p";
30+
// Initialises a string variable with the value "399p".
31+
// Example value now: "399p"
32+
33+
// const penceStringWithoutTrailingP = penceString.substring(
34+
// 0,
35+
// penceString.length - 1
36+
// );
37+
38+
39+
// What it does: takes a substring of penceString from index 0 up to (but not including) penceString.length - 1.
40+
// Why: removes the last character (the trailing "p") so we are left with just the numeric part.
41+
// Example: "399p".substring(0, 4 - 1) → "399".
42+
// Result stored: penceStringWithoutTrailingP === "399"
43+
44+
// const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
45+
46+
47+
// 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"
51+
52+
// const pounds = paddedPenceNumberString.substring(
53+
// 0,
54+
// paddedPenceNumberString.length - 2
55+
// );
56+
57+
58+
// 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:
97+
98+
// "5p" → "5" → pad → "005" → pounds = "0", pence = "05" → £0.05
99+
100+
// "45p" → "45" → pad → "045" → pounds = "0", pence = "45" → £0.45
101+
102+
// 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:
109+
110+
let numericPence = parseInt(penceString.replace(/\D/g, ""), 10);
111+
pounds = Math.floor(numericPence / 100);
112+
pence = String(numericPence % 100).padStart(2, "0");
113+
114+
115+
// 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.
118+

0 commit comments

Comments
 (0)