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
// I predict the code will give an error because the sum function does not return a value.
4
4
functionsum(a,b){
5
5
return;
6
6
a+b;
@@ -9,5 +9,12 @@ function sum(a, b) {
9
9
console.log(`The sum of 10 and 32 is ${sum(10,32)}`);
10
10
11
11
// =============> write your explanation here
12
+
// The function sum(a, b) has a return statement that does not return the result of a + b.
13
+
// Instead, it returns undefined because the return statement is followed by a semicolon, which ends the statement before the addition operation is executed.
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)}`);
Copy file name to clipboardExpand all lines: Sprint-2/2-mandatory-debug/2.js
+12Lines changed: 12 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 function has no parameter, so it cannot use input values like 42, 105, 806
5
6
6
7
constnum=103;
7
8
@@ -15,10 +16,21 @@ 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 out will be the last digit of 103 for all three console.log statements, because the function getLastDigit() is using the variable num which is set to 103, instead of using the input values passed to it.
18
20
// Explain why the output is the way it is
19
21
// =============> write your explanation here
22
+
//The function is not using the value passed into it.
20
23
// Finally, correct the code to fix the problem
21
24
// =============> write your new code here
25
+
functiongetLastDigit(num){
26
+
returnnum.toString().slice(-1);
27
+
}
28
+
29
+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
30
+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
31
+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
22
32
23
33
// This program should tell the user the last digit of each number.
24
34
// Explain why getLastDigit is not working properly - correct the problem
35
+
// If a function should work with different values → it must have parameters.
36
+
// Otherwise it will always use the same fixed value
0 commit comments