File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments