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/2-mandatory-errors/3.js
+13-1Lines changed: 13 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,18 @@
1
1
constcardNumber=4533787178994213;
2
-
constlast4Digits=cardNumber.slice(-4);
3
2
3
+
constlast4Digits=String(cardNumber).slice(-4);
4
+
5
+
6
+
console.log(last4Digits);// "4213"
7
+
8
+
9
+
letprediction=" because of the lack of slice method available for numbers,I predict that the code will throw an error. the cardNumber variable is a number, and slice is a method that can only be used on strings or arrays.";
@@ -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
+
//one function call in line 4 ,5
15
16
16
17
// 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?
18
+
// there seem to be no error when running the code.
17
19
18
20
// c) Identify all the lines that are variable reassignment statements
21
+
// line 4 and 5
19
22
20
23
// d) Identify all the lines that are variable declarations
24
+
// 1,2,7,8
21
25
22
26
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
27
+
28
+
// The expression `Number(carPrice.replaceAll(",", ""))` is performs two operations. it uses the `replaceAll` method to remove all commas from the string `carPrice`, effectively converting it from a formatted string o a plain number string.
29
+
//Then, it converts that resulting string into a number using the `Number()` function.
30
+
//The purpose of this expression is to convert a formatted price string into a numerical value that can be used for calculations.
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
22
-
25
+
// it means that the movie hour is being calculated. totalminutes minus remainingseconds divided by 60.
23
26
// e) What do you think the variable result represents? Can you think of a better name for this variable?
24
-
27
+
// the variable result represents the total length of the movie in hours and minutes.
28
+
//Better name is MOvieTime.
25
29
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
30
+
// Yes, this code will work for all values because it is using the modulus operator to calculate the remaining seconds and is using division to calculate the total hours.
31
+
// The code will correctly handle any length of movie in seconds and convert it to the appropriate format.
// Try and describe the purpose / rationale behind each step
25
25
26
26
// To begin, we can start with
27
-
// 1. const penceString = "399p": initialises a string variable with the value "399p"
27
+
// 1. const penceString = "399p": // the "399p"represents the amount in pence.
28
+
// initializes a string variable with the value "399p"
29
+
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): // the trailing 'p'has been removed from the string to isolate the numeric value.
30
+
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): // ensures that the numeric value has at least three digits by padding with leading zeros .
31
+
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): // extracts the pounds portion of the string by taking all but the last two characters.
32
+
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): // extracts the pence portion of the string by taking the last two characters and ensures it has two digits by padding with trailing zeros .
33
+
// 6. console.log(`£${pounds}.${pence}`): // outputs the final formatted price in pounds and pence to the console, prefixed with the pound symbol (£).
0 commit comments