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
//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.
//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.
16
+
17
+
//When running the program we have to understand how the expression works:
18
+
19
+
//we have to work out the parentheses first, maximum - minimum + 1, which is equal to 100 - 1 + 1 = 100.
20
+
//so: Math.floor(Math.random() *100) + 1
21
+
// 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
+
//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.
25
+
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.
28
+
//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
+
//After adding 1, we need to round down the number to the nearest interger, this is done by using Math.floor(). After this we will have a whole number between maximum and minimum.
30
+
//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
3
+
3
4
// want the computer to run these 2 lines - how can we solve this problem?
4
5
5
-
Wecanadd2forwardslashes,bytypingtwoforwardslashes(// ) at the beginning of the line, we can turn that line into a comment. This then means that the computer will completely ignore that line and not run it.
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.
//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.
//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.
//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
+
//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.
// 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
+
16
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?
17
-
SyntaxError: missingacommatoseparatethearguments
19
+
20
+
//>SyntaxError: missing a comma to separate the arguments
21
+
18
22
// c) Identify all the lines that are variable reassignment statements
//>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.
//>movieLength % 60 means finding the remainder after dividing movieLength by 60. movieLength = 8784 so we need to do: 8784 % 60 = 146.4 and the nearest whole integer is 146.
24
+
//So there are 146 complete groups of 60. we then multiply; 146 x 60= 8760 --> 8784 - 8760 = 24 so 8784 % 60 = 24 and therefore remainingSeconds becomes 24.
25
+
//8784/60 = 146.4. and 8784 % 60 = 24 as this is the remainder.
23
26
24
27
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
//>const totalMinutes = (movieLength - remainingSeconds) / 60 ---> if we break it down -> movieLength = 8784 remainingSeconds = 24
29
+
//we then subtract remainingSeconds from movieLength= 8784 -24 = 8670. we then have to divide it by 60 which gives us 8760 / 60 = 146. we divide because there is 60 seconds in one minute, and right now we are converting seconds into minutes.
30
+
27
31
// e) What do you think the variable result represents? Can you think of a better name for this variable?
0 commit comments