Skip to content

Commit 063ef21

Browse files
completed and corrected the incorrect codes in mandatory-debug coursework
1 parent 2ec33a8 commit 063ef21

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

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

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

33
// =============> write your prediction here
4-
4+
// as we are not returning anything from the function, it is not possible to retrieve the returned value from this function, so it will throw undefined.
55
function multiply(a, b) {
66
console.log(a * b);
77
}
88

99
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1010

1111
// =============> write your explanation here
12-
12+
// however when the function is run, it displays 320 and undefined next to the The result of multiplying 10 and 32. So obviusly the 320 is displayed on the console, because that's the output of the function, but it is undefined when we try to retrieve it for use in the template literals. so we need to return the value inside a function in order to use it outside.
1313
// Finally, correct the code to fix the problem
1414
// =============> write your new code here
15+
function multiply(a, b) {
16+
return a * b;
17+
}
18+
19+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3-
3+
// the function is returned without calculating anything, and as we know after return, the function stops executing and anything after return is not executable. so it will throw undefined.
44
function sum(a, b) {
55
return;
66
a + b;
@@ -9,5 +9,11 @@ function sum(a, b) {
99
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1010

1111
// =============> write your explanation here
12+
// the function has to also return a value or the output in order to use this same value in the global scope.
1213
// Finally, correct the code to fix the problem
1314
// =============> write your new code here
15+
function sum(a, b) {
16+
return a + b;
17+
}
18+
19+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

Lines changed: 11 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+
// the num is declared in the global scope and the function will use it despite when it is called with different argruments and thus will bring the result to become only 3.
66
const num = 103;
77

88
function getLastDigit() {
@@ -15,10 +15,20 @@ 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+
// 3 in all the 3 function calls
1819
// Explain why the output is the way it is
1920
// =============> write your explanation here
21+
// because it is the global scope declaration of num the function will use it as it doesn't have its own num declared inside.
2022
// Finally, correct the code to fix the problem
2123
// =============> write your new code here
2224

25+
function getLastDigit(num) {
26+
return num.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)}`);
32+
// the num can be declared as a parameter so as the function can work for any argument passed to it.
2333
// This program should tell the user the last digit of each number.
2434
// Explain why getLastDigit is not working properly - correct the problem

0 commit comments

Comments
 (0)