Skip to content

Commit dd98827

Browse files
Fix function definition and undefined variable error
1 parent f0d2c5a commit dd98827

File tree

1 file changed

+21
-1
lines changed
  • Sprint-2/1-key-errors

1 file changed

+21
-1
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,40 @@
11

22
// Predict and explain first BEFORE you run any code...
33

4+
// The code has a syntax error because a number is used instead of a parameter name in the function definition.
5+
// Additionally, num is used inside the function without being declared or passed as an argument, which causes a ReferenceError.
6+
// If the function is not called, it will not execute.
47
// this function should square any number but instead we're going to get an error
58

69
// =============> write your prediction of the error here
710

8-
function square(3) {
11+
// The code has a syntax error because a number is used instead of a parameter name in the function definition.
12+
// Additionally, num is used inside the function without being declared or passed as an argument, which causes a ReferenceError.
13+
// If the function is not called, it will not execute.
14+
15+
function square(num) {
916
return num * num;
1017
}
18+
let num = square(3);
19+
console.log(num);
20+
1121

1222
// =============> write the error message here
23+
// syntax error :unexpected number.
1324

1425
// =============> explain this error message here
26+
// The error occurs because a number is used in the function definition instead of a parameter name. A function definition must contain a parameter (a variable name), not a value.
27+
28+
// Inside the function, num is used but it was never declared or passed as a parameter, which causes a ReferenceError because num is not defined in the scope.
29+
30+
// Also, if the function is not called, it will not execute.
1531

1632
// Finally, correct the code to fix the problem
1733

1834
// =============> write your new code here
1935

36+
function square(num) {
37+
return num * num;
38+
}
2039

40+
console.log(square(3));

0 commit comments

Comments
 (0)