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
// Why? Because we don't pass any parameter to the function ant it always uses the variable "num", which defined in the global scope, so function has access to it and the last digit of 103 is 3.
5
9
6
-
constnum=103;
10
+
// const num = 103;
7
11
8
12
functiongetLastDigit(){
9
13
returnnum.toString().slice(-1);
@@ -14,11 +18,26 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14
18
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15
19
16
20
// Now run the code and compare the output to your prediction
17
-
// =============> write the output here
21
+
// =============> The last digit of 42 is 3
22
+
// The last digit of 105 is 3
23
+
// The last digit of 806 is 3
18
24
// Explain why the output is the way it is
19
-
// =============> write your explanation here
25
+
// =============> Because we use the global variable "num" in the function instead of passing the parameter to the function.
20
26
// Finally, correct the code to fix the problem
21
-
// =============> write your new code here
22
-
27
+
//=============> const num = 103;
28
+
// function getLastDigit(num) {
29
+
// return num.toString().slice(-1);
30
+
// }
31
+
// console.log(`The last digit of 42 is ${getLastDigit(42)}`);
32
+
// console.log(`The last digit of 105 is ${getLastDigit(105)}`);
33
+
// console.log(`The last digit of 806 is ${getLastDigit(806)}`);
34
+
//
23
35
// This program should tell the user the last digit of each number.
36
+
//
37
+
// Now output looks correctly:
38
+
// The last digit of 42 is 2
39
+
// The last digit of 105 is 5
40
+
// The last digit of 806 is 6
41
+
//
24
42
// Explain why getLastDigit is not working properly - correct the problem
43
+
// Actually, maybe its not the good idea to use the same name "num" for different variables in different scopes, because it can be confusing. It works correctly but next time I'll use for the global scope some more meaningful name.
0 commit comments