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
Copy file name to clipboardExpand all lines: Sprint-1/3-mandatory-interpret/1-percentage-change.js
+16Lines changed: 16 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -13,10 +13,26 @@ console.log(`The percentage change is ${percentageChange}`);
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
15
16
+
// 1. carPrice.replaceAll(",", "")
17
+
// 2. priceAfterOneYear.replaceAll(",", "")
18
+
// 3. Number(...)
19
+
// 4. console.log(...) -
20
+
// 5. (priceDifference / carPrice) * 100 - line 7 (this is an expression that involves division and multiplication, but it does not involve a function call)
21
+
16
22
// 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?
17
23
24
+
// The error is on line 5 where `priceAfterOneYear.replaceAll(",", "")` is called. The syntax error is due to a missing closing quote in the `replaceAll` method.
25
+
// Fix: Change `priceAfterOneYear.replaceAll(",", "")` to `priceAfterOneYear.replaceAll(",", "")`
26
+
18
27
// c) Identify all the lines that are variable reassignment statements
19
28
29
+
// Line 4: carPrice = Number(carPrice.replaceAll(",", ""));
30
+
// Line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
31
+
20
32
// d) Identify all the lines that are variable declarations
21
33
34
+
// Line 1: let carPrice = "10,000";
35
+
// Line 2: let priceAfterOneYear = "8,543";
36
+
22
37
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
38
+
// The expression `carPrice.replaceAll(",", "")` removes all commas from the string `carPrice`, converting it to a number that can be used in mathematical operations. This is necessary because the original value is a string with commas (e.g., "10,000"), which cannot be directly used in arithmetic calculations. The `Number()` function then converts the resulting string (e.g., "10000") into a numeric value.
// The expression `movieLength % 60` calculates the remainder when `movieLength` is divided by 60. This is used to determine the number of seconds that are left after accounting for the full minutes in the movie length. For example, if `movieLength` is 8784 seconds, then `movieLength % 60` would give us the remaining seconds after converting as many full minutes as possible from the total seconds.
20
23
21
24
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
25
+
// The expression assigned to `totalMinutes` is calculating the total number of full minutes in the movie length. It does this by first subtracting the `remainingSeconds` from `movieLength`, which gives us the total number of seconds that can be fully converted into minutes. Then, it divides that result by 60 to convert those seconds into minutes. For example, if `movieLength` is 8784 seconds and `remainingSeconds` is 24 seconds, then `totalMinutes` would be calculated as (8784 - 24) / 60, which equals 145 minutes.
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 time in hours, minutes, and seconds. A better name for this variable could be `formattedTime`.
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`. It correctly calculates the hours, minutes, and seconds for any given length of time in seconds. However, if `movieLength` is negative, the calculations would not make sense in the context of a movie length, and the output would be incorrect. Therefore, it is important to ensure that `movieLength` is a non-negative integer for this code to function properly.
// This program takes a string representing a price in pence
21
+
// It then removes the trailing "p" character, pads the number with leading
22
+
// zeros to ensure it has at least 3 digits, and then separates the pounds and pence parts of the number.
23
+
21
24
// The program then builds up a string representing the price in pounds
25
+
// and pence format (e.g., "£3.99") by concatenating the pounds and pence parts together with a "£" symbol and a decimal point. The final output is printed to the console.
26
+
27
+
// This program takes a string representing a price in pence (e.g., "399p") and converts it into a formatted string representing the price in pounds and pence (e.g., "£3.99").
28
+
// by concatenating the pounds and pence parts together with a "£" symbol and a decimal point. The final output is printed to the console.
22
29
23
30
// You need to do a step-by-step breakdown of each line in this program
31
+
// a) How many function calls are there in this file? Write down all the lines where a function call is made
32
+
// 1. penceString.substring(0, penceString.length - 1) - line 3
33
+
// 2. penceStringWithoutTrailingP.padStart(3, "0") - line 5
34
+
// 3. paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2) - line 6
35
+
// 4. paddedPenceNumberString.substring(paddedPenceNumberString.length - 2) - line 9
36
+
// 5. .padEnd(2, "0") - line 9
37
+
// 6. console.log(`£${pounds}.${pence}`)' - line 11
38
+
24
39
// Try and describe the purpose / rationale behind each step
40
+
// 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?
41
+
// c) Identify all the lines that are variable reassignment statements
42
+
// d) Identify all the lines that are variable declarations
43
+
// e) Describe what the expression penceString.substring(0, penceString.length - 1) is doing - what is the purpose of this expression?
25
44
26
45
// To begin, we can start with
27
-
// 1. const penceString = "399p": initialises a string variable with the value "399p"
46
+
// a) How many function calls are there in this file? Write down all the lines where a function call is made
47
+
48
+
// 1. penceString.substring(0, penceString.length - 1) - line 3
49
+
// 1. const penceString = "399p": initialises a string variable with the val
50
+
// ue "399p".
51
+
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): creates a new string variable by taking a substring of `penceString` that excludes the last character (the "p"). This effectively removes the trailing "p" from the original string, leaving just the numeric part (e.g., "399").
52
+
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): pads the `penceStringWithoutTrailingP` with leading zeros to ensure it has at least 3 characters. If `penceStringWithoutTrailingP` is shorter than 3 characters, it will add zeros at the start until it reaches a length of 3 (e.g., "399" remains "399", but "99" would become "099").
0 commit comments