Skip to content

Commit 87950af

Browse files
committed
completion of 1-key-errors
1 parent 8cfcf96 commit 87950af

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

Sprint-2/2-mandatory-debug/0.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
// Predict and explain first...
22

33
// =============> write your prediction here
4+
// The error will occur because the function 'sum' is not returning the result of multiplying 'a' and 'b'. Instead, it is returning 'undefined' due to the function being incomplete. The error message will likely indicate that the result of the sum is 'undefined' when we try to log it to the console.
45

5-
function multiply(a, b) {
6-
console.log(a * b);
7-
}
6+
//function multiply(a, b) {
7+
//console.log(a * b);
8+
//}
89

9-
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
10+
//console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1011

1112
// =============> write your explanation here
13+
// The error is occurring because the function 'multiply' is not returning any value. In JavaScript, if a function does not explicitly return a value, it returns 'undefined' by default. To fix this problem, we need to add a return statement to the function that returns the result of multiplying 'a' and 'b'. For example, we could change the line to: return a * b; so that the function will return the correct result when called.
1214

1315
// Finally, correct the code to fix the problem
1416
// =============> write your new code here
17+
function multiply(a, b) {
18+
return a * b;
19+
}
20+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
21+

Sprint-2/2-mandatory-debug/1.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
//The error will occur because the function 'sum' is not returning the result of summing 'a' and 'b'. Instead, it is returning 'undefined' due to the function being incomplete. The error message will likely indicate that the result of the sum is 'undefined' when we try to log it to the console.
4+
//function sum(a, b) {
5+
//return;
6+
//a + b;
7+
//}
38

4-
function sum(a, b) {
5-
return;
6-
a + b;
7-
}
8-
9-
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
9+
//console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1010

1111
// =============> write your explanation here
12+
// The error is occurring because the function 'sum' is not returning any value. In JavaScript, if a function does not explicitly return a value, it returns 'undefined' by default. To fix this problem, we need to add a return statement to the function that returns the result of summing 'a' and 'b'. For example, we could change the line to: return a + b; so that the function will return the correct result when called.
13+
14+
1215
// Finally, correct the code to fix the problem
1316
// =============> write your new code here
17+
function sum(a, b) {
18+
return a + b;
19+
}
20+
21+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

Sprint-2/2-mandatory-debug/2.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,37 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
//the output will be: an error because the function getLastDigit is not defined to take any parameters, but we are trying to pass a number as an argument when we call the function. The error message will likely indicate that getLastDigit is not a function or that it cannot be called with arguments.
56

6-
const num = 103;
7+
//const num = 103;
78

8-
function getLastDigit() {
9-
return num.toString().slice(-1);
10-
}
9+
//function getLastDigit() {
10+
//return num.toString().slice(-1);
11+
//}
1112

12-
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
13-
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14-
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
13+
//console.log(`The last digit of 42 is ${getLastDigit(42)}`);
14+
//console.log(`The last digit of 105 is ${getLastDigit(105)}`);
15+
//console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1516

1617
// Now run the code and compare the output to your prediction
1718
// =============> write the output here
19+
//it does run but returns the last digit of the number 103 for all three calls to getLastDigit, which is not the expected behavior. The output will be:
20+
// The last digit of 42 is 3
21+
// The last digit of 105 is 3
22+
// The last digit of 806 is 3
23+
1824
// Explain why the output is the way it is
1925
// =============> write your explanation here
26+
// The function getLastDigit is not taking any parameters, so it always returns the last digit of the global variable 'num', which is 103. The function should take a number as a parameter and return the last digit of that number.
27+
2028
// Finally, correct the code to fix the problem
2129
// =============> write your new code here
30+
function getLastDigit(num) {
31+
return num.toString().slice(-1);
32+
}
33+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
34+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
35+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2236

2337
// This program should tell the user the last digit of each number.
2438
// Explain why getLastDigit is not working properly - correct the problem

0 commit comments

Comments
 (0)