Skip to content

Commit be9e3ec

Browse files
committed
Fixed square() parameter error, added MDN explanation and test cases
1 parent 8a3c7d2 commit be9e3ec

File tree

1 file changed

+17
-0
lines changed
  • Sprint-2/1-key-errors

1 file changed

+17
-0
lines changed

Sprint-2/1-key-errors/2.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,34 @@
44
// this function should square any number but instead we're going to get an error
55

66
// =============> write your prediction of the error here
7+
// Prediction: It will throw a syntax error because 3 is not a valid parameter name.
78

9+
/* Original Code:
810
function square(3) {
911
return num * num;
1012
}
13+
*/
1114

1215
// =============> write the error message here
16+
// SyntaxError: Unexpected number
1317

1418
// =============> explain this error message here
19+
// MDN Reference: https://developer.mozilla.org/en-US/docs/Glossary/Identifier
20+
21+
// JavaScript does not allow numbers to be used as variable names or parameter names.
22+
// Function parameters must follow the rules for valid identifiers.
23+
// According to MDN, an identifier may not start with a digit and must follow JavaScript's naming rules.
1524

1625
// Finally, correct the code to fix the problem
1726

1827
// =============> write your new code here
28+
function square(num) {
29+
return num * num;
30+
}
31+
32+
console.log(square(3)); // 9
1933

34+
// Test cases:
2035

36+
// console.log(square(10)); // 100
37+
// console.log(square(5)); // 25

0 commit comments

Comments
 (0)