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
Adjustments made to the files in Sprint-2/2-mandatory-debug/0.js, Sprint-2/2-mandatory-debug/1.js, and Sprint-2/2-mandatory-debug/2.js have been staged for commit. The changes include fixing the sum function to return the correct result and correcting the getLastDigit function to use the parameter instead of a global variable.
Copy file name to clipboardExpand all lines: Sprint-2/2-mandatory-debug/0.js
+9Lines changed: 9 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,7 @@
1
1
// Predict and explain first...
2
2
3
3
// =============> write your prediction here
4
+
// The command is to multiply two numbers and print the result. However, the function multiply does not return any value, it only logs the product to the console. Therefore, when we try to use the result of multiply in a template literal, it will be undefined, which will lead to an incorrect output.
4
5
5
6
functionmultiply(a,b){
6
7
console.log(a*b);
@@ -9,6 +10,14 @@ function multiply(a, b) {
9
10
console.log(`The result of multiplying 10 and 32 is ${multiply(10,32)}`);
10
11
11
12
// =============> write your explanation here
13
+
// The function multiply takes two parameters, a and b, and logs their product to the console. However, it does not return any value, which means that when we try to use the result of multiply in the template literal, it will be undefined. This will lead to the output: "The result of multiplying 10 and 32 is undefined". To fix this, we need to modify the multiply function to return the product instead of just logging it.
12
14
13
15
// Finally, correct the code to fix the problem
14
16
// =============> write your new code here
17
+
functionmultiply(a,b){
18
+
returna*b;
19
+
}
20
+
21
+
console.log(`The result of multiplying 10 and 32 is ${multiply(10,32)}`);
22
+
23
+
// Now the function multiply returns the product of a and b, and the output will be: "The result of multiplying 10 and 32 is 320".
Copy file name to clipboardExpand all lines: Sprint-2/2-mandatory-debug/1.js
+9Lines changed: 9 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,6 @@
1
1
// Predict and explain first...
2
2
// =============> write your prediction here
3
+
// The command is to calculate the sum of two numbers and print the result. However, the function sum does not return any value, it only has a return statement without an expression. Therefore, when we try to use the result of sum in a template literal, it will be undefined, which will lead to an incorrect output.
3
4
4
5
functionsum(a,b){
5
6
return;
@@ -9,5 +10,13 @@ function sum(a, b) {
9
10
console.log(`The sum of 10 and 32 is ${sum(10,32)}`);
10
11
11
12
// =============> write your explanation here
13
+
// The function sum takes two parameters, a and b, and has a return statement without an expression. This means that the function will return undefined when called. When we try to use the result of sum in the template literal, it will be undefined, which will lead to the output: "The sum of 10 and 32 is undefined". To fix this, we need to modify the sum function to return the actual sum of a and b instead of just having a return statement without an expression.
12
14
// Finally, correct the code to fix the problem
13
15
// =============> write your new code here
16
+
functionsum(a,b){
17
+
returna+b;
18
+
}
19
+
20
+
console.log(`The sum of 10 and 32 is ${sum(10,32)}`);
21
+
22
+
// Now the function sum returns the actual sum of a and b, and the output will be: "The sum of 10 and 32 is 42".
Copy file name to clipboardExpand all lines: Sprint-2/2-mandatory-debug/2.js
+20Lines changed: 20 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,7 @@
2
2
3
3
// Predict the output of the following code:
4
4
// =============> Write your prediction here
5
+
// The code will output: "The last digit of 42 is 3", "The last digit of 105 is 3", and "The last digit of 806 is 3". This is because the function getLastDigit is using the variable num, which is set to 103, instead of the parameter passed to the function. Therefore, it will always return the last digit of 103, which is 3.
5
6
6
7
constnum=103;
7
8
@@ -15,10 +16,29 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15
16
16
17
// Now run the code and compare the output to your prediction
17
18
// =============> write the output here
19
+
// The output is:
20
+
// The last digit of 42 is 3
21
+
// The last digit of 105 is 3
22
+
// The last digit of 806 is 3
18
23
// Explain why the output is the way it is
19
24
// =============> write your explanation here
25
+
// The output is the way it is because the function getLastDigit is not using the parameter passed to it. Instead, it is using the variable num, which is set to 103. Therefore, regardless of the input, the function will always return the last digit of 103, which is 3. To fix this issue, we need to modify the function to use the parameter instead of the variable num.
20
26
// Finally, correct the code to fix the problem
21
27
// =============> write your new code here
28
+
functiongetLastDigit(num){
29
+
returnnum.toString().slice(-1);
30
+
}
31
+
32
+
// Now the function getLastDigit will return the last digit of the number passed as an argument, and the output will be:
33
+
// The last digit of 42 is 2
34
+
// The last digit of 105 is 5
35
+
// The last digit of 806 is 6
22
36
23
37
// This program should tell the user the last digit of each number.
24
38
// Explain why getLastDigit is not working properly - correct the problem
39
+
// The function getLastDigit is not working properly because it is using a global variable num instead of the parameter passed to the function. To correct this problem, we need to change the function definition to accept a parameter and use that parameter to calculate the last digit. The corrected function should look like this:
40
+
functiongetLastDigit(num){
41
+
returnnum.toString().slice(-1);
42
+
}
43
+
44
+
// Now the function will work correctly and return the last digit of the input number.
0 commit comments