Skip to content

Commit fc9b43b

Browse files
committed
mandatory-debug done
1 parent 482253a commit fc9b43b

3 files changed

Lines changed: 56 additions & 15 deletions

File tree

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

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

33
// =============> write your prediction here
4+
// logging the result of the function will always be undefined, since it doesn't return anything.
5+
// if the inputs are two numbers, should console log the right number, e.g. (2, 2) -> 4 (2.5, 2.5) -> 6.25
6+
// otherwise, it might produce incorrect results
47

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

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

1114
// =============> write your explanation here
15+
// Javascript tries to cast non-number values to numbers before multiplying. So, "3" to 3 for example.
16+
// if the value can't be converted to a number, it might cause unpredictable or incorrect outputs.
17+
// e.g. Boolean true is converted to numeric 1, so true * 2 becomes 1 * 2 = 2.
1218

1319
// Finally, correct the code to fix the problem
1420
// =============> write your new code here
21+
function multiply(a, b) {
22+
if (Number.isFinite(a) && Number.isFinite(b)) {
23+
return a * b;
24+
}
25+
throw new TypeError("Arguments must be finite numbers");
26+
}
27+
28+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

Sprint-2/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 output of console log should be 'The sub of 10 and 32 is undefined'
34

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

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

1112
// =============> write your explanation here
13+
// since the return statement is the first thing in the function body, and it returns nothing.
14+
// the return value of the function is undefined.
15+
// so in the console log, it will coerce undefined to string, and print that.
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+
}
22+
23+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,40 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
// The output will always be 3
56

67
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+
// The last digit of 42 is 3
20+
// The last digit of 105 is 3
21+
// The last digit of 806 is 3
22+
1823
// Explain why the output is the way it is
1924
// =============> write your explanation here
25+
// The function does not take an argument, so supplying one is no use.
26+
// Since there is no num variable inside the function, it looks for it in the nearest enclosing scope.
27+
// It finds num = 103, so it will always operate on that variable
28+
2029
// Finally, correct the code to fix the problem
2130
// =============> write your new code here
31+
function getLastDigit(num) {
32+
return num.toString().slice(-1);
33+
}
34+
35+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
36+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
37+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2238

2339
// This program should tell the user the last digit of each number.
2440
// Explain why getLastDigit is not working properly - correct the problem
41+
// ??? it returns the last digit

0 commit comments

Comments
 (0)