Skip to content

Commit e7b50d9

Browse files
committed
Complete error exercise 2 - square parameter
1 parent 6128237 commit e7b50d9

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

  • Sprint-3/1-key-errors

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
1-
21
// Predict and explain first BEFORE you run any code...
3-
42
// this function should square any number but instead we're going to get an error
5-
63
// =============> write your prediction of the error here
4+
// Prediction: SyntaxError — you cannot use a number literal (3) as a parameter name.
5+
// Parameter names must be valid identifiers (e.g. num), not values.
6+
// The value 3 should be passed when calling the function, not in the definition.
77

8-
function square(3) {
9-
return num * num;
10-
}
8+
//function square(3) {
9+
//return num * num;
10+
//}
1111

1212
// =============> write the error message here
13+
// SyntaxError: Unexpected number
14+
// (exact wording can vary slightly by Node version, e.g. "Missing formal parameter")
1315

1416
// =============> explain this error message here
17+
// In a function definition, the parentheses list parameter names (placeholders).
18+
// 3 is a value (argument), not a name, so JavaScript rejects it while parsing the file.
19+
// Even if that were fixed, num is not defined in the original code — the parameter
20+
// and the name used inside the function must match.
1521

1622
// Finally, correct the code to fix the problem
17-
1823
// =============> write your new code here
24+
function square(num) {
25+
return num * num;
26+
}
1927

28+
console.log(square(3));
2029

0 commit comments

Comments
 (0)