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/1-key-exercises/1-count.js
+1Lines changed: 1 addition & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -4,3 +4,4 @@ count = count + 1;
4
4
5
5
// Line 1 is a variable declaration, creating the count variable with an initial value of 0
6
6
// Describe what line 3 is doing, in particular focus on what = is doing
7
+
// line 3 is updating the value of the count variable by adding 1 to its current value. In other words, it is re-assignining the count variable to be equal to its previous value plus 1. It is called a statement.
// In this exercise, you will need to work out what num represents?
7
+
// num represents a random integer between the minimum and maximum values, inclusive. The expression math.random() picks any decinmal number between 0 and 1, where then it's multiplied by the change of min & max + 1. Then the Math.floor() rounds the number down to the nearest integer and finally the minimum value is added to ensure the result is withn the specified range.
8
+
7
9
// Try breaking down the expression and using documentation to explain what it means
10
+
/* Math.random() generates a random decimal number between 0 (inclusive) and 1(not inclusive).
11
+
The expression (maximum - minimum +1) calculates the range of possible values, which is 100 - 1 + 1 = 100.
12
+
Then the result of Math.random() is multiplied by the range, which gives a random decimal number between 0 and 100 (not inclusive).
13
+
Math.floor() rounds the result down to the nearest integer, which gives a random integer between 0 and 99 (inclusive).
14
+
Finally, adding the minimum value of 1 shifts the range up by 1, resulting in a random integer between 1 and 100 (inclusive). */
15
+
8
16
// It will help to think about the order in which expressions are evaluated
9
17
// Try logging the value of num and running the program several times to build an idea of what the program is doing
// The last4Digits variable should store the last 4 digits of cardNumber
5
5
// However, the code isn't working
6
6
// Before running the code, make and explain a prediction about why the code won't work
7
+
// The code won't work because cardNumber is a number, and the slice method is a string method. So the cardNumber value needs to be coverted to a string. It can be done by using the cardNumber.toString() method or by using string(cardNumber) method or even by using template literals as shown in the code snippet.
8
+
7
9
// Then run the code and see what error it gives.
10
+
// The error says cardNumber.slice is not a function. This is becuase the slice method is a string method and cardNumber is a number.
11
+
8
12
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
13
+
// At first I thought the error would be that last4Digits is not defined, but I learned that the error is actually because the slice method is being called on a number, which doesn't have that method.
9
14
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
15
+
constlast4Digits=cardNumber.toString().slice(-4);// first option
16
+
constlast4Digits=String(cardNumber).slice(-4);// second option
17
+
constlast4Digits=`${cardNumber}`.slice(-4);// third option
@@ -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 code snippet.
16
+
1) carPrice = Number(carPrice.replaceAll(",", "")); --Number(...) & replaceAll(",", "") in this line
17
+
2) priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); --Number(...) & replaceAll("," "") in this line as well.
18
+
3) console.log(`The percentage change is ${percentageChange}`);
19
+
*/
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
+
/* The error shown says ) missing after arguements in the expression of priceAfterOneYear. However when I looked into it carefully, the comma was missing between the arguments, not the ). I also used debugger in vscode and simply pointed out the problem by as on the debug console suggested.
23
+
*/
17
24
18
25
// c) Identify all the lines that are variable reassignment statements
0 commit comments