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-2/1-key-exercises/1-count.js
+1-3Lines changed: 1 addition & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,5 @@ count = count + 1;
6
6
7
7
// Describe what line 3 is doing, in particular focus on what = is doing
8
8
9
-
10
-
11
9
//Line 1 is a declaration and Line 3 is a statement that changes the value of the variable 'count' by adding 1 to its current value.
12
-
//The variable is changing from 0 to 1 after line 3 is executed after we used 'let' to declare the variable 'count' and then we used the = operator to assign the new value.
10
+
//The variable is changing from 0 to 1 after line 3 is executed after we used 'let' to declare the variable 'count' and then we used the = operator to assign the new value.
// In this exercise, you will need to work out what num represents?
10
9
// Try breaking down the expression and using documentation to explain what it means
11
10
// It will help to think about the order in which expressions are evaluated
12
11
// Try logging the value of num and running the program several times to build an idea of what the program is doing
13
12
14
13
//Num represents a random whole number between the value of 'minimum' (1) and 'maximum' (100)
15
-
//By creating a constant variable called 'num' and assigning it the value of the expression, we can generate a random number within the mentioned range.
14
+
//By creating a constant variable called 'num' and assigning it the value of the expression, we can generate a random number within the mentioned range.
16
15
17
16
//When running the program we have to understand how the expression works:
18
17
19
18
//we have to work out the parentheses first, maximum - minimum + 1, which is equal to 100 - 1 + 1 = 100.
20
19
//so: Math.floor(Math.random() *100) + 1
21
20
// Math.floor is used 1 time and it happens before + minimum. Math.floor is used because when used Math.random() it produces a decimal number between 0 and 1.
22
21
//The code needs a whole number because we are trying to get a number between 1 and 100, this is where Math.floor comes () comes in and removes the decimal part.
23
-
//JavaScript works out the expression inside Math.floor () first before + minimum.
24
-
//Math.random() produces a pseudo-random decimal number that is greater than or equal to 0 but less than 1.
22
+
//JavaScript works out the expression inside Math.floor () first before + minimum.
23
+
//Math.random() produces a pseudo-random decimal number that is greater than or equal to 0 but less than 1.
25
24
26
-
//Next, we need to multiply the result from Math.random() by 100. This may give us the result of a decimal between 0 and 100.
27
-
//Next step is to use Math.floor() wich then rounds down the decimal number to the nearest whole integer. This then means that the result will be a whole number between 0 and 99.
25
+
//Next, we need to multiply the result from Math.random() by 100. This may give us the result of a decimal between 0 and 100.
26
+
//Next step is to use Math.floor() wich then rounds down the decimal number to the nearest whole integer. This then means that the result will be a whole number between 0 and 99.
28
27
//But we want 1-100, so we add 1 to the result of Math.floor() this then changes the range to be inclusive of 1 and 100.
29
28
//Therefore, the final result of num will be a random whole number between 1 and 100, inclusive.
//This is just an instruction for the first activity - but it is just for human consumption
2
-
//We don't
2
+
//We don't
3
3
4
4
// want the computer to run these 2 lines - how can we solve this problem?
5
5
6
6
//We can add 2 forward slashes , by typing two forward slashes ( // ) at the beginning of the line, we can turn both lines into a comment. This then means that the computer will completely ignore that line and not run it.
// trying to create an age variable and then reassign the value by 1
2
-
letage=33;
2
+
letage=33;
3
3
age=age+1;
4
4
5
-
console.log(age);
5
+
console.log(age);
6
6
7
-
8
-
9
-
//const means that the variable age is a constant and cannot be reassigned. When we try to reassign the value of age by adding 1 to it, we get an error.
10
-
//This is because we cannot change the value of a constant variable once we have created it.
7
+
//const means that the variable age is a constant and cannot be reassigned. When we try to reassign the value of age by adding 1 to it, we get an error.
8
+
//This is because we cannot change the value of a constant variable once we have created it.
9
+
//// The error is a TypeError: Assignment to constant variable.
11
10
12
11
//If we want to change the value of age, we need to use let instead of consts when declaring the variable. this then allows us to reassign the value of age by adding 1 to it.
// 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
-
//error
4
+
// The error is a ReferenceError: Cannot access 'cityOfBirth' before initialization
5
5
6
6
//console.log(`I was born in ${cityOfBirth}`);
7
7
//const cityOfBirth = "Bolton";
8
8
9
9
//The reason there is an error is because we are using the wrong order, we are telling Javascript to print the value of cityOfBirth before creating a variable.
10
10
//So we need to create the variable first, this is because Javascript runs code from top to bottom, so a variable that is declared with const needs to be declared before using it.
@@ -12,7 +12,7 @@ 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
-
//>
15
+
//>
16
16
// There are 5 function calls, two function calls on line 4 = Number(...) and replaceAll(...) two more on line 5 = Number(...) and replaceAll(...) and finally on line 10 = console.log(...)
17
17
18
18
// 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?
@@ -23,8 +23,8 @@ console.log(`The percentage change is ${percentageChange}`);
23
23
//>carPrice = Number(carPrice.replaceAll(",", "")); and priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
24
24
25
25
// d) Identify all the lines that are variable declarations
26
-
//>There are 4 variable declarations- 1) let carPrice = "10,000"; 2) let priceAfterOneYear = "8,543";
26
+
//>There are 4 variable declarations- 1) let carPrice = "10,000"; 2) let priceAfterOneYear = "8,543";
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
30
-
//>Number(carPrice.replaceAll(",", "")); is a string and the expression removes the comma from "10,000" and converts "10000" from a string into the number 10000.
30
+
//>Number(carPrice.replaceAll(",", "")); is a string and the expression removes the comma from "10,000" and converts "10000" from a string into the number 10000.
0 commit comments