Skip to content

Commit ed77449

Browse files
author
Pretty Taruvinga
committed
predicted, explained and fixed the code
1 parent 94da557 commit ed77449

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3-
3+
// I predict that the error will occur because the function 'sum' does not return any value, so when we try to use it inside the template literal, it will return 'undefined'.
4+
// This will result in the output being "The sum of 10 and 32 is undefined" instead of the expected sum of 10 and 32.
45
function sum(a, b) {
56
return;
67
a + b;
@@ -9,5 +10,12 @@ function sum(a, b) {
910
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1011

1112
// =============> write your explanation here
13+
// The error occurs because the function 'sum' has a return statement that does not return any value, which means it returns 'undefined' by default.
14+
// When we try to use the result of 'sum(10, 32)' inside the template literal, it evaluates to 'undefined', which is not the expected output.
15+
// To fix this error, we need to change the return statement in the 'sum' function to return the sum of 'a' and 'b'.
16+
1217
// Finally, correct the code to fix the problem
1318
// =============> write your new code here
19+
function sum(a, b) {
20+
return a + b;
21+
}

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5-
5+
// I predict that the output will be "The last digit of 42 is 3", "The last digit of 105 is 3", and "The last digit of 806 is 3" because the function 'getLastDigit' is using the variable 'num' which is set to 103, so it will always return the last digit of 103, which is 3, regardless of the input passed to the function.
66
const num = 103;
77

88
function getLastDigit() {
@@ -15,10 +15,25 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1515

1616
// Now run the code and compare the output to your prediction
1717
// =============> write the output here
18+
// The output is "The last digit of 42 is 3", "The last digit of 105 is 3", and "The last digit of 806 is 3". This matches my prediction because the function 'getLastDigit' is using the variable 'num' which is set to 103, so it will always return the last digit of 103, which is 3, regardless of the input passed to the function.
1819
// Explain why the output is the way it is
1920
// =============> write your explanation here
21+
// The output is the way it is because the function 'getLastDigit' is not using the parameter 'number' that is passed to it.
22+
// Instead, it is using the variable 'num' which is set to 103.
23+
// Since 'num' is not changing based on the input, the function will always return the last digit of 103, which is 3, regardless of the input passed to the function. To fix this issue, we need to change the function to use the parameter 'number' instead of the variable 'num'.
2024
// Finally, correct the code to fix the problem
2125
// =============> write your new code here
26+
const num = 103;
27+
28+
function getLastDigit(number) {
29+
return number.toString().slice(-1);
30+
}
31+
32+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
33+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
34+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2235

2336
// This program should tell the user the last digit of each number.
2437
// Explain why getLastDigit is not working properly - correct the problem
38+
// GetLastDigit seems not to work properly because the variable num is never passed into the function.
39+
// The fix is to call getLastDigit(num) so the function uses that value.

0 commit comments

Comments
 (0)