File tree Expand file tree Collapse file tree 1 file changed +12
-0
lines changed Expand file tree Collapse file tree 1 file changed +12
-0
lines changed Original file line number Diff line number Diff line change 11// Predict and explain first...
22// =============> write your prediction here
3+ // The variable str is being declared twice within the same scope, which will cause a syntax error.
34
45// call the function capitalise with a string input
56// interpret the error message and figure out why an error is occurring
@@ -8,6 +9,17 @@ function capitalise(str) {
89 let str = `${ str [ 0 ] . toUpperCase ( ) } ${ str . slice ( 1 ) } ` ;
910 return str ;
1011}
12+ const result = capitalise ( 'hello' ) ;
13+ console . log ( result ) ;
14+ // SyntaxError: Identifier 'str' has already been declared
1115
1216// =============> write your explanation here
17+ // // The first declaration is in the function parameter and the second one is inside the function body.
18+ // In JavaScript, you cannot declare the same variable name in the same scope using let or const.
19+ // To fix this, we can either rename the inner variable or assign the capitalised value to the parameter itself without redeclaring it.
20+
1321// =============> write your new code here
22+ function capitalise ( str ) {
23+ str = `${ str [ 0 ] . toUpperCase ( ) } ${ str . slice ( 1 ) } ` ;
24+ return str ;
25+ }
You can’t perform that action at this time.
0 commit comments