Skip to content

Commit 4542624

Browse files
committed
2.js committed
1 parent 2a91f72 commit 4542624

File tree

1 file changed

+27
-6
lines changed
  • Sprint-2/1-key-errors

1 file changed

+27
-6
lines changed

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

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,41 @@
11

22
// Predict and explain first BEFORE you run any code...
3-
43
// this function should square any number but instead we're going to get an error
5-
64
// =============> write your prediction of the error here
75

8-
function square(3) {
9-
return num * num;
10-
}
6+
/**
7+
* Prediction:
8+
* The function parameter is incorrectly defined as a numeric literal 3 instead of a parameter name. This will cause a syntax error because JavaScript function parameters must be valid identifiers (variable names), not numbers.
9+
*/
10+
11+
/** Original function:
12+
*
13+
* function square(3) {
14+
* return num * num;
15+
* }
16+
*/
17+
1118

1219
// =============> write the error message here
20+
/**
21+
* Error message:
22+
* Uncaught SyntaxError: Unexpected number
23+
*/
1324

1425
// =============> explain this error message here
26+
/** Explanation:
27+
* In JavaScript, when defining a function, the parameters must be valid variable names (like num, x, value, etc.). Using a literal number like 3 as a parameter is invalid syntax because:
28+
* Parameters act as placeholders for values that will be passed when the function is called
29+
* Numbers can not be used as variable names in JavaScript
30+
* The parser expects a valid identifier in the parameter declaration, not a numeric literal
31+
*/
1532

1633
// Finally, correct the code to fix the problem
17-
1834
// =============> write your new code here
1935

36+
function square(num) {
37+
return num * num;
38+
}
39+
40+
console.log(square(3));
2041

0 commit comments

Comments
 (0)