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
@@ -12,11 +12,20 @@ console.log(`The percentage change is ${percentageChange}`);
12
12
// Read the code and then answer the questions below
13
13
14
14
// a) How many function calls are there in this file? Write down all the lines where a function call is made
15
+
// Answer: There are 5 function/method calls across 3 lines:
16
+
// - Line 4: replaceAll(",", "") and Number(...)
17
+
// - Line 5: replaceAll(",", "") and Number(...)
18
+
// - Line 10: console.log(...)
15
19
16
-
// b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem?
20
+
// b) Run the code and identify the line where the error is coming from – why is this error occurring? How can you fix this problem?
21
+
// Answer: On line 5, there is a typo in replaceAll(",", "") where the comma is outside the quotes or contains empty space before the closing quote (""")).
22
+
// Fix line 5 by changing it to: priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
17
23
18
24
// c) Identify all the lines that are variable reassignment statements
25
+
// Answer: Lines 4 and 5 (carPrice = ... and priceAfterOneYear = ...)
19
26
20
27
// d) Identify all the lines that are variable declarations
28
+
// Answer: Lines 1, 2, 7, and 8 (let carPrice, let priceAfterOneYear, const priceDifference, const percentageChange)
21
29
22
-
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
30
+
// e) Describe what the expression Number(carPrice.replaceAll(",", "")) is doing – what is the purpose of this expression?
31
+
// Answer: .replaceAll(",", "") removes the comma from the price string ("10,000" becomes "10000"), and Number(...) converts that clean string into a numeric value so mathematical calculations can be performed on it
// Answer: % is the remainder (modulo) operator. It calculates the leftover seconds when movieLength is divided into full minutes (60-second chunks).
20
22
21
23
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
24
+
// Answer: It subtracts the leftover seconds from the total movie length to get a exact multiple of 60, then divides by 60 to convert that duration into whole minutes.
22
25
23
26
// e) What do you think the variable result represents? Can you think of a better name for this variable?
27
+
// Answer: It represents the movie length formatted as a HH:MM:SS time string. A clearer name would be formattedTime or formattedMovieLength.
24
28
25
29
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
30
+
// Answer: It works for all non-negative numbers representing total seconds. However, if movieLength is negative or not a number (e.g., a string or null), it will output invalid time values or NaN.
// You need to do a step-by-step breakdown of each line in this program
24
24
// Try and describe the purpose / rationale behind each step
25
25
26
-
// To begin, we can start with
27
-
// 1. const penceString = "399p": initialises a string variable with the value "399p"
26
+
// 1. const penceString = "399p": Initialises a string variable with the value "399p" representing the price in pence including the trailing letter 'p'.
27
+
28
+
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): Extracts the numeric portion of the pence string by taking characters from index 0 up to (but not including) the last character, removing the 'p' ("399").
29
+
30
+
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): Ensures the pence string is at least 3 digits long by adding leading zeros if necessary (e.g., "5" becomes "005"), ensuring there are enough digits to extract both pounds and pence.
31
+
32
+
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): Extracts the pounds portion of the price by taking all digits except the last two ("3").
33
+
34
+
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): Extracts the last two digits representing pence ("99") and ensures it is padded to 2 digits.
35
+
36
+
// 6. console.log(`£${pounds}.${pence}`): Formats and prints the final price string with the pound symbol, pounds amount, period, and pence amount ("£3.99").
0 commit comments