You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Predict and explain first... /*There will be a syntax error in the code because the variable `str` is being declared twice within the same scope. The first declaration is in the function parameter, and the second declaration is inside the function body. This will cause a "SyntaxError: Identifier 'str' has already been declared" error.*?
2
+
// =============> write your prediction here // there will be an error message as such SyntaxError: Identifier 'str' has already been declared
3
+
4
+
// call the function capitalise with a string input// capitalise("tobias");
5
+
// interpret the error message and figure out why an error is occurring, /*
6
+
7
+
/*function capitalise(str) {
8
+
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
16
+
^
17
+
18
+
SyntaxError: Identifier 'str' has already been declared
19
+
at Object.compileFunction (node:vm:353:18)
20
+
at wrapSafe (node:internal/modules/cjs/loader:1039:15)
21
+
at Module._compile (node:internal/modules/cjs/loader:1073:27)
22
+
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
23
+
at Module.load (node:internal/modules/cjs/loader:989:32)
24
+
at Function.Module._load (node:internal/modules/cjs/loader:829:14)
25
+
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
26
+
at node:internal/main/run_main_module:17:47*/
27
+
// from the error message above the error occurred at line 8 where the variable `str` was declared again using `let` inside the function body, which is not allowed since `str` was already declared as a parameter of the function.
28
+
// to fix this error, we can either rename the variable inside the function body or remove the `let` keyword and just assign a new value to `str` without redeclaring it.
/* There will be a syntax error in the code because the variable `decimalNumber` is being declared twice within the same scope. The first declaration is in the function parameter, and the second declaration is inside the function body. This will cause a "SyntaxError: Identifier 'decimalNumber' has already been declared" error. secondly variable `decimalNumber` is being logged to the console outside of the function, which will also cause a ReferenceError since `decimalNumber` is not defined in that scope.*/
3
+
4
+
// Why will an error occur when this program runs?/* The variable 'decimalNumber ' is being redeclared inside the function, which is not allowed since it was already declared as a parameter of the function. And the variable `decimalNumber` is being logged to the console outside of the function, which will cause a ReferenceError since `decimalNumber` is not defined in that scope.*/
5
+
// =============> write your prediction here. // This will cause a syntax error, additionally also cause a reference error since the variable `decimalNumber` is being logged to the console outside of the function, which will also cause a ReferenceError since `decimalNumber` is not defined in that scope.
6
+
7
+
8
+
// Try playing computer with the example to work out what is going on
0 commit comments