Skip to content

Commit 1b1b8c9

Browse files
committed
update: Predict and explain code, then fix square function to correct parameter definition and error explanation in Sprint-2/1-key-errors/2.js
1 parent 2302130 commit 1b1b8c9

File tree

1 file changed

+14
-8
lines changed
  • Sprint-2/1-key-errors

1 file changed

+14
-8
lines changed

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
21
// Predict and explain first BEFORE you run any code...
3-
2+
// the will be an error
43
// this function should square any number but instead we're going to get an error
54

5+
// Why will an error occur when this program runs?
6+
//because the function parameter is incorrectly defined by real number instead of a variable name
7+
68
// =============> write your prediction of the error here
79

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

1214
// =============> write the error message here
13-
15+
//syntaxError: Unexpected number
1416
// =============> explain this error message here
15-
17+
// The error occurs because the function parameter is defined as a number (3) instead of a variable name.
18+
// In JavaScript, function parameters must be variable names, not literal values. JavaScript is expecting a parameter name in the parentheses, not a value.
1619
// Finally, correct the code to fix the problem
1720

1821
// =============> write your new code here
1922

20-
23+
function square(num) {
24+
return num * num;
25+
}
26+
console.log(square(3)); // This will not work because 'num' is not defined outside the function

0 commit comments

Comments
 (0)