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
// Currently trying to print the string "I was born in Bolton" but it isn't working...
2
2
// what's the error ?
3
3
4
-
console.log(`I was born in ${cityOfBirth}`);
4
+
5
+
/*console.log(`I was born in ${cityOfBirth}`);
6
+
const cityOfBirth = "Bolton";*/
7
+
/* The error is that the variable `cityOfBirth` is being used before it is declared and assigned a value. In JavaScript, variables declared with `const` (or `let`) are not hoisted in the same way as `var`, so you cannot access them before their declaration. To fix this, you should declare and assign the variable before using it in the `console.log` statement:*/
8
+
//The variable were being accessed from the Temporal Dead Zone (TDZ) before it was declared and assigned a value. In JavaScript, variables declared with `const` (or `let`) are not hoisted in the same way as `var`, so you cannot access them before their declaration. To fix this, you should declare and assign the variable before using it in the `console.log` statement:
// The last4Digits variable should store the last 4 digits of cardNumber
5
9
// However, the code isn't working
6
10
// Before running the code, make and explain a prediction about why the code won't work
7
11
// Then run the code and see what error it gives.
8
12
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
9
13
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
14
+
// I suspect the code doesn't work because the slice method is being called on a number, but slice is a method for strings. Therefore, I predict that the code will throw an error saying that slice is not a function for numbers.
15
+
16
+
// i console the typeof cardNumber and it returns number
/*names beginning with $ or _ often carry special conventions:
4
+
5
+
_name often implies a private/internal variable.
6
+
$name is commonly associated with libraries like jQuery or special framework objects.*/
7
+
/*For ordinary variables, it's usually clearer to start with a letter and use camelCase and avoid special characters, not starting with a number. and choosing meaningful names that describe the variable's purpose.*/
@@ -12,11 +12,30 @@ 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. The function calls are made on the following lines:
16
+
// Line 1: replaceAll()
17
+
// Line 2: replaceAll()
18
+
// Line 5: Number()
19
+
// Line 6: Number()
20
+
// Line 9: console.log()
15
21
16
-
// 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
22
23
+
// 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?
24
+
// The error is occurring on line 5., and it's a SyntaxError: missing , The error is due to a missing comma in the replaceAll() method.//The error occurs on line 5. A SyntaxError is thrown because a comma is missing between the two arguments passed to the replaceAll() method. JavaScript uses commas to separate arguments in a function call. The programming term that belongs to the blank is call arguments as that is the actual value passed during a function call. The correct syntax should be replaceAll(",", ""). To fix this problem, we need to add the missing comma in the replaceAll() method on line 5
18
25
// c) Identify all the lines that are variable reassignment statements
26
+
// The variable reassignment statements are on the following lines:
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 variable declaration statements are on the following lines:
32
+
// Line 1: let carPrice = "10,000";
33
+
// Line 2: let priceAfterOneYear = "8,543";
34
+
// Line 7: const priceDifference = carPrice - priceAfterOneYear;
// A declaration is when a variable is created, using let or const.
37
+
38
+
21
39
22
40
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
41
+
/* The expression Number(carPrice.replaceAll(",","")) is converting the string value of carPrice, which contains a comma, into a number. The replaceAll() method is used to remove all commas from the string, and then the Number() function is used to convert the resulting string into a number. This allows for mathematical operations to be performed on the value of carPrice without any issues caused by the presence of commas in the string.*/
0 commit comments