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,19 @@ 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
+
// 5 function calls
16
+
// replaceAll is called twice on line 4 and line 5
17
+
// Number is called twice on line 4 and line 5
18
+
// console.log is called once on line 7
15
19
16
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
+
// its coming from line 4 and line 5 because we are trying to call the replaceAll method on a string that contains a comma, which is not a valid number. To fix this problem, we can remove the commas from the strings before converting them to numbers. We can do this by using the replaceAll method to replace all commas with an empty string before calling the Number function. This is already done in the code, so there should be no error when running it.
17
22
18
23
// c) Identify all the lines that are variable reassignment statements
24
+
// line 4 and 5
19
25
20
26
// d) Identify all the lines that are variable declarations
27
+
// line 1, 2, 7, 8.
21
28
22
29
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
30
+
// The expression Number(carPrice.replaceAll(",","")) is first calling the replaceAll method on the carPrice string to remove all commas, and then it is converting the resulting string to a number using the Number function. The purpose of this expression is to convert the carPrice string, which may contain commas, into a numeric value that can be used for calculations.
// The expression movieLength % 60 calculates the remainder when movieLength is divided by 60. This is used to determine how many seconds are left after accounting for the full minutes in the movie length. For example, if movieLength is 8784 seconds, then 8784 % 60 would give us the number of seconds that do not make up a full minute, which is 24 seconds in this case.
20
23
21
24
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
25
+
// The expression (movieLength - remainingSeconds) / 60 calculates the total number of minutes in the movie length by subtracting the remaining seconds from the total seconds and then dividing by 60.
22
26
23
27
// e) What do you think the variable result represents? Can you think of a better name for this variable?
28
+
// The variable result represents the formatted string of the movie length in hours, minutes, and seconds. A better name for this variable could be formattedMovieLength or movieLengthFormatted to make it more descriptive of its purpose.
24
29
25
30
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
31
+
// This code will work for all non-negative integer values of movieLength. However, if movieLength is negative, the calculations for remainingSeconds, totalMinutes, remainingMinutes, and totalHours may not produce meaningful results.
// 1. const penceString = "399p": initialises a string variable with the value "399p"
28
+
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): creates a new string variable that contains the original string without the last character (the "p"), resulting in "399"
29
+
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): pads the string with leading zeros to ensure it has at least 3 characters, resulting in "399"
30
+
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): extracts the substring representing the pounds by taking all characters except the last two, resulting in "3"
31
+
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): extracts the last two characters to represent the pence and pads it with trailing zeros if necessary, resulting in "99"
32
+
// 6. console.log(`£${pounds}.${pence}`): outputs the formatted price in pounds and pence, resulting in "£3.99"
0 commit comments