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,27 @@ 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
+
// there are 5 function calls in this file. including methods used , 2 methods = replaceAll(), methods are called under their object, replaceAll() is called under the string object.
16
+
// The lines where a function call is made are:
17
+
// Line 4: carPrice.replaceAll(",", "")
18
+
// Line 5: priceAfterOneYear.replaceAll(",", "")
19
+
// Line 7: console.log(`The percentage change is ${percentageChange}`)
15
20
16
21
// 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?
22
+
//syntaxError: missing ) after argument list, -
23
+
//the error is occurring because of a missing "," in the replaceAll() method. in line 5.
17
24
18
25
// c) Identify all the lines that are variable reassignment statements
26
+
// The lines that are variable reassignment statements are:
27
+
// Line 4: carPrice = Number(carPrice.replaceAll(",", ""));
28
+
// Line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
19
29
20
30
// d) Identify all the lines that are variable declarations
31
+
// The lines that are variable declarations are:
32
+
// Line 1: let carPrice = "10,000";
33
+
// Line 2: let priceAfterOneYear = "8,543";
34
+
// Line 7: const priceDifference = carPrice - priceAfterOneYear;
// The expression movieLength % 60 represents the remainder of the division of movieLength by 60.
23
+
// In this case, it calculates the number of seconds remaining after converting the total movie length from seconds to minutes.
24
+
// The modulo operator (%) returns the remainder of a division operation, so it helps to determine how many seconds are left after accounting for full minutes in the movie length.
25
+
// this means 8784 seconds is exactly 146 minutes and 24 seconds.
20
26
21
27
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
28
+
// The expression assigned to totalMinutes will return the total number of minutes only leaving the seconds out.
22
29
23
30
// e) What do you think the variable result represents? Can you think of a better name for this variable?
31
+
// the variable holds the hour,minutes and seconds a movie is long, so i prefer to name it movieDuration.
24
32
25
33
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
34
+
// yes, this code will work for all values of movieLength because every attributes of the movieLength variable is being calculated and converted to hours, minutes and seconds.
// 1. const penceString = "399p": initialises a string variable with the value "399p"
28
+
//2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): removes the trailing "p" from the string to get the numeric part of the price in pence
29
+
//3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): pads the string with leading zeros to ensure it has at least 3 characters, which is useful for prices less than £1
30
+
//4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): extracts the substring representing the pounds by taking all characters/values except the last two
31
+
//5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): extracts the substring representing the pence by taking the last two characters and padding it with a "0" if necessary
32
+
//6. console.log(`£${pounds}.${pence}`): outputs the final formatted price in pounds and pence to the console
0 commit comments