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
// =============> return on its own means that the function ends without returning a value, so the result is undefined.
3
+
/*
4
4
function sum(a, b) {
5
5
return;
6
6
a + b;
7
7
}
8
8
9
9
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
10
+
*/
10
11
11
12
// =============> write your explanation here
13
+
// return on it's own means that the function wont refer to parameters and will come up as undefined. a+ b is never reached because it comes after return.
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-3/2-mandatory-debug/2.js
+22-1Lines changed: 22 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,11 @@
1
1
// Predict and explain first...
2
2
3
3
// Predict the output of the following code:
4
-
// =============> Write your prediction here
4
+
// =============> The last digit of 42 is 3
5
+
// The last digit of 105 is 3
6
+
// The last digit of 806 is 3
5
7
8
+
/*
6
9
const num = 103;
7
10
8
11
function getLastDigit() {
@@ -12,13 +15,31 @@ function getLastDigit() {
12
15
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
13
16
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14
17
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
18
+
*/
15
19
16
20
// Now run the code and compare the output to your prediction
17
21
// =============> write the output here
22
+
/*The last digit of 42 is 3
23
+
The last digit of 105 is 3
24
+
The last digit of 806 is 3
25
+
*/
18
26
// Explain why the output is the way it is
19
27
// =============> write your explanation here
28
+
//The output is this way as there is a num assigned before the function and function don't use the parameter num,
29
+
//so the output would only be the return of slice value for const num before the function.
30
+
20
31
// Finally, correct the code to fix the problem
21
32
// =============> write your new code here
33
+
constnum=103;
34
+
35
+
functiongetLastDigit(num){
36
+
returnnum.toString().slice(-1);
37
+
}
38
+
39
+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
40
+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
41
+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
22
42
23
43
// This program should tell the user the last digit of each number.
24
44
// Explain why getLastDigit is not working properly - correct the problem
45
+
//Function was not working correctly as it was returning num slice for const num that was assigned before the function, function should use parameter num to work correctly.
0 commit comments