File tree Expand file tree Collapse file tree 3 files changed +41
-18
lines changed
Sprint-2/2-mandatory-debug Expand file tree Collapse file tree 3 files changed +41
-18
lines changed Original file line number Diff line number Diff line change 11// Predict and explain first...
22
3- // =============> write your prediction here
3+ // =============> Prediction:
4+ // The program will print "The result of multiplying 10 and 32 is undefined"
5+ // because the function multiply does not return a value, it only prints to the console.
46
57function multiply ( a , b ) {
68 console . log ( a * b ) ;
79}
810
911console . log ( `The result of multiplying 10 and 32 is ${ multiply ( 10 , 32 ) } ` ) ;
1012
11- // =============> write your explanation here
13+ // =============> Explanation:
14+ // 1. The function multiply(a, b) uses console.log inside the function, but it does not return anything.
15+ // 2. When we use ${multiply(10, 32)} inside the string, it tries to use the return value of multiply.
16+ // 3. Since there is no return, JavaScript treats it as undefined.
1217
13- // Finally, correct the code to fix the problem
14- // =============> write your new code here
18+ // =============> Corrected Code:
19+ // We need the function to return the result instead of printing it.
20+
21+ function multiply ( a , b ) {
22+ return a * b ;
23+ }
24+
25+ console . log ( `The result of multiplying 10 and 32 is ${ multiply ( 10 , 32 ) } ` ) ;
26+ // Output: "The result of multiplying 10 and 32 is 320"
Original file line number Diff line number Diff line change 11// Predict and explain first...
2- // =============> write your prediction here
2+
3+ // =============> Prediction:
4+ // The program will print "The sum of 10 and 32 is undefined"
5+ // because the function returns nothing due to the 'return;' line.
36
47function sum ( a , b ) {
58 return ;
@@ -8,6 +11,17 @@ function sum(a, b) {
811
912console . log ( `The sum of 10 and 32 is ${ sum ( 10 , 32 ) } ` ) ;
1013
11- // =============> write your explanation here
12- // Finally, correct the code to fix the problem
13- // =============> write your new code here
14+ // =============> Explanation:
15+ // 1. In JavaScript, as soon as the function hits 'return;' it stops and returns undefined.
16+ // 2. The line 'a + b;' is never reached because it's after the return statement.
17+ // 3. Therefore, the function does not actually return the sum.
18+
19+ // =============> Corrected Code:
20+ // We need to return the sum directly.
21+
22+ function sum ( a , b ) {
23+ return a + b ;
24+ }
25+
26+ console . log ( `The sum of 10 and 32 is ${ sum ( 10 , 32 ) } ` ) ;
27+ // Output: "The sum of 10 and 32 is 42"
Original file line number Diff line number Diff line change 11// Predict and explain first...
22
33// Predict the output of the following code:
4- // =============> Write your prediction here
4+ // =============> Prediction:
5+ // The program will print:
6+ // The last digit of 42 is 3
7+ // The last digit of 105 is 3
8+ // The last digit of 806 is 3
9+ // because the function always uses the constant 'num' (103) and ignores the numbers we pass.
510
611const num = 103 ;
712
@@ -13,12 +18,4 @@ console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1318console . log ( `The last digit of 105 is ${ getLastDigit ( 105 ) } ` ) ;
1419console . log ( `The last digit of 806 is ${ getLastDigit ( 806 ) } ` ) ;
1520
16- // Now run the code and compare the output to your prediction
17- // =============> write the output here
18- // Explain why the output is the way it is
19- // =============> write your explanation here
20- // Finally, correct the code to fix the problem
21- // =============> write your new code here
22-
23- // This program should tell the user the last digit of each number.
24- // Explain why getLastDigit is not working properly - correct the problem
21+ // ===========
You can’t perform that action at this time.
0 commit comments