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
// The program then builds up a string representing the price in pounds
25
34
// 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
35
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").
36
+
// 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
37
// by concatenating the pounds and pence parts together with a "£" symbol and a decimal point. The final output is printed to the console.
29
38
30
39
// You need to do a step-by-step breakdown of each line in this program
// 2. penceStringWithoutTrailingP.padStart(3, "0") - line 5
34
43
// 3. paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2) - line 6
35
44
// 4. paddedPenceNumberString.substring(paddedPenceNumberString.length - 2) - line 9
36
-
// 5. .padEnd(2, "0") - line 9
37
-
// 6. console.log(`£${pounds}.${pence}`)' - line 11
45
+
// 5. padEnd(2, "0") - line 9
46
+
// 6. console.log(`£${pounds}.${pence}`) - line 11
38
47
39
48
// Try and describe the purpose / rationale behind each step
40
49
// 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
50
// c) Identify all the lines that are variable reassignment statements
42
51
// d) Identify all the lines that are variable declarations
43
52
// e) Describe what the expression penceString.substring(0, penceString.length - 1) is doing - what is the purpose of this expression?
44
53
45
-
// To begin, we can start with
46
-
// a) How many function calls are there in this file? Write down all the lines where a function call is made
54
+
// a) How many function calls are there in this file?
47
55
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