Skip to content

Commit 2008962

Browse files
completed the mandatory debug file
1 parent bb65455 commit 2008962

3 files changed

Lines changed: 55 additions & 16 deletions

File tree

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

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

33
// =============> write your prediction here
4+
// This function would multiply the two arguments a and b whatever that is
5+
// when we log the output we will get the string and the output of the function but because we are not returning an output we might run into an error
46

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

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

1113
// =============> write your explanation here
14+
//The console.log() inside the funtion will print a*b but when we call the function will return undefined
1215

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

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// The function suppose to return the sum of the two variables but we will encounter an error as the code after return is unreachable.
34

4-
function sum(a, b) {
5-
return;
6-
a + b;
7-
}
85

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

1112
// =============> write your explanation here
13+
14+
// As we return nothing by closing the line with a semicolon hence a + b is not returned where we tell the computer to exit and go back to global scope and the computer will not be able to read the lines after that
15+
1216
// Finally, correct the code to fix the problem
1317
// =============> write your new code here
18+
19+
function sum(a, b) {
20+
return a + b;
21+
}
22+
23+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

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

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
//getLastDigit will convert a number to a string
6+
// However, since there is no parameter or local variable to the function we might run into an Error
57

6-
const num = 103;
8+
// const num = 103;
79

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

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)}`);
14+
// console.log(`The last digit of 42 is ${getLastDigit(42)}`);
15+
// console.log(`The last digit of 105 is ${getLastDigit(105)}`);
16+
// console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1517

1618
// Now run the code and compare the output to your prediction
1719
// =============> write the output here
20+
// The last digit of 42 is 3
21+
// The last digit of 105 is 3
22+
// The last digit of 806 is 3
1823
// Explain why the output is the way it is
1924
// =============> write your explanation here
25+
// As expected, the function has no parameters, so it relies on the global variable "num" as its only variable.
26+
// Since there are no parameters defined, the function permanently uses the global variable instead.
27+
// This means even if we pass arguments when calling the function, they will be ignored because there are no parameters to receive them.
28+
2029
// Finally, correct the code to fix the problem
2130
// =============> write your new code here
2231

32+
function getLastDigit(num) {
33+
return num.toString().slice(-1);
34+
}
35+
36+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
37+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
38+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
39+
2340
// This program should tell the user the last digit of each number.
2441
// Explain why getLastDigit is not working properly - correct the problem
42+
/* the reason why getLastDigit isn't working properly is that it doesn't accept any arrguments where there isn't any perameters in its difination.
43+
where it uses the golabal varaible 'num' decalred outside the funtion and already set to 103
44+
45+
The correct code includes a parameter 'num' in the difination this allows the function to accept the number passed to it when called*/

0 commit comments

Comments
 (0)