File tree Expand file tree Collapse file tree 1 file changed +20
-3
lines changed
Expand file tree Collapse file tree 1 file changed +20
-3
lines changed Original file line number Diff line number Diff line change 1- // Predict and explain first...
1+ // Predict and explain first...
2+ //Syntax error
23// =============> write your prediction here
4+ // We pass str as a parameter to the function.
5+ //The parameter becomes a local variable inside the function scope.
6+ //Therefore, we cannot declare another variable with the same name inside the same scope.
37
48// call the function capitalise with a string input
59// interpret the error message and figure out why an error is occurring
610
711function capitalise ( str ) {
8- let str = `${ str [ 0 ] . toUpperCase ( ) } ${ str . slice ( 1 ) } ` ;
12+ str = `${ str [ 0 ] . toUpperCase ( ) } ${ str . slice ( 1 ) } ` ;
913 return str ;
1014}
11-
15+ let result = capitalise ( "Arun" ) ;
16+ console . log ( result ) ;
1217// =============> write your explanation here
18+ // The parameter str is already a local variable inside the function.
19+ // Previously, redeclaring it using let caused an error because variables cannot be declared twice in the same scope.
20+ // Instead of redeclaring it, I modified the existing str variable and returned it.
21+ // This avoids the redeclaration error and the code runs correctly.
1322// =============> write your new code here
23+ function capitalise ( str ) {
24+ str = `${ str [ 0 ] . toUpperCase ( ) } ${ str . slice ( 1 ) } ` ;
25+ return str ;
26+ }
27+ let str = capitalise ( "Arun" ) ;
28+ console . log ( str ) ;
29+
30+
You can’t perform that action at this time.
0 commit comments