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
@@ -20,3 +21,18 @@ console.log(`The percentage change is ${percentageChange}`);
20
21
// d) Identify all the lines that are variable declarations
21
22
22
23
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
24
+
25
+
/*
26
+
a) There are two functions Line 4 & 5:
27
+
for eg, The expression Number(carPrice.replaceAll(",","")) is doing the following:
28
+
1. carPrice.replaceAll(",","") - This removes any commas from the carPrice string.
29
+
2. Number(...) - This converts the resulting string into a number.
30
+
b) The uncaught syntax error is coming from line 5. The error occurs because there is a missing comma in the replaceAll method.
31
+
It should be carPrice.replaceAll(",", "") instead of carPrice.replaceAll("," ""). To fix this, add the missing comma.
32
+
c) The variable reassignment statements are on lines 4 and 5, where carPrice and priceAfterOneYear are being reassigned to their numeric values.
33
+
d) The variable declarations are on lines 1 and 2, where carPrice and priceAfterOneYear are declared using the let keyword.
34
+
35
+
e) The purpose of the expression Number(carPrice.replaceAll(",", "")) is to convert a string representation of a number (with commas as thousands separators) into an actual number that can be used in mathematical operations.
36
+
37
+
Generally, the purpose of this expression is to convert a string representation of a number (with commas as thousands separators) into a actual number that can be used in mathematical operations.
Copy file name to clipboardExpand all lines: Sprint-1/3-mandatory-interpret/2-time-format.js
+14Lines changed: 14 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -23,3 +23,17 @@ console.log(result);
23
23
// e) What do you think the variable result represents? Can you think of a better name for this variable?
24
24
25
25
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
26
+
27
+
/*
28
+
a) There are 6 variable declarations in this program: movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, and result.
29
+
30
+
b) There is 1 function call in this program: console.log(result).
31
+
32
+
c) The expression movieLength % 60 calculates the remainder when movieLength is divided by 60. This is used to determine the number of seconds that do not make up a full minute.
33
+
34
+
d) The expression assigned to totalMinutes calculates the total number of minutes in the movie by subtracting the remaining seconds from the total length of the movie and then dividing by 60. This gives the whole number of minutes.
35
+
e) The variable result represents the formatted string of the movie length in hours, minutes, and seconds. A better name for this variable could be movieDuration.
36
+
37
+
f) The code will work for all non-negative values of movieLength. However, if movieLength is negative, the calculations for remainingSeconds, totalMinutes, remainingMinutes, and totalHours may not produce meaningful results.
38
+
Additionally, if movieLength is not an integer, the calculations may also end up with unexpected results.
// 1. const penceString = "399p": initialises a string variable with the value "399p"
28
+
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): this removes the last character of the string (the "p"), leaving just the digits "399".
29
+
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): this pads the string with leading zeros until it is at least 3 characters long. Since "399" is already 3 characters, it stays "399". This step matters for smaller pence values, e.g. "9p" would become "009", ensuring there are always at least 2 digits left for pence.
30
+
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): this takes all characters except the last 2, giving the pounds part of the amount. For "399", this gives "3".
31
+
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): this takes the last 2 characters of the padded string (the pence part), then pads it with trailing zeros if it's shorter than 2 characters. For "399", this gives "99".
32
+
// 6. console.log(£${pounds}.${pence}): this combines the pounds and pence parts into a formatted price string, e.g. "£3.99".
0 commit comments