Skip to content

Commit 135ce97

Browse files
committed
Update 0.js
Questions Answered
1 parent 3372770 commit 135ce97

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

  • Sprint-2/1-key-errors

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// I predict when this code runs, there will be a SyntaxError before the function executes.
3+
// This is because the identifier 'str' has already been declared.
34

45
// call the function capitalise with a string input
6+
// capitalise("hello");
7+
8+
59
// interpret the error message and figure out why an error is occurring
10+
// Uncaught SyntaxError: Unexpected identifier 'string' - this is the error message.
11+
// This error is occuring because the variable 'str' is being redeclared within the function, which is not allowed in JavaScript.
12+
// The duplicate use if 'str' is causing the syntax error.
13+
14+
15+
//function capitalise(str) {
16+
//let str = `${str[0].toUpperCase()}${str.slice(1)}`;
17+
//return str;
18+
//}
19+
20+
// The issue is variable redeclaration.
21+
// str is the function parameter.
22+
// let str tries to create a new variable with the same name.
23+
// JavaScript does not allow redeclaring a variable in the same scope.
24+
// As a result, the code throws a SyntaxError when it encounters the second 'str' declaration.
25+
26+
// New code without the error:
627

7-
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9-
return str;
10-
}
1128

12-
// =============> write your explanation here
13-
// =============> write your new code here

0 commit comments

Comments
 (0)