File tree Expand file tree Collapse file tree 1 file changed +19
-4
lines changed
Expand file tree Collapse file tree 1 file changed +19
-4
lines changed Original file line number Diff line number Diff line change 44// call the function capitalise with a string input
55// interpret the error message and figure out why an error is occurring
66
7- function capitalise ( str ) {
8- let str = `${ str [ 0 ] . toUpperCase ( ) } ${ str . slice ( 1 ) } ` ;
9- return str ;
10- }
7+ // function capitalise(str) {
8+ // let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9+ // return str;
10+ // }
1111
1212// =============> write your explanation here
13+ // The function capitalize is trying to take a string input and return it with the first letter capitalized.
14+ // However, the error occurs because the variable 'str' is being declared twice: once as a parameter and again inside the function.
15+ // This is not allowed in JavaScript, as 'let' does not allow redeclaration of the same variable in the same scope.
16+ // This causes a syntax error because you cannot redeclare a variable with 'let' in the same scope.
1317// =============> write your new code here
18+
19+ function capitalise ( str ) {
20+ let name = `${ str [ 0 ] . toUpperCase ( ) } ${ str . slice ( 1 ) } ` ;
21+ return name ;
22+ }
23+ capitalise ( "hello, check " ) ;
24+ console . log ( capitalise ( "hello, check " ) ) ;
25+ // =============> write your explanation here
26+ // The function now uses a different variable name 'name' instead of 'str' inside the function.
27+ // This avoids the redeclaration error. The function takes a string input, capitalizes the first letter, and returns the modified string.
28+ // However, the return statement still returns 'str' instead of 'name', which is a mistake.
You can’t perform that action at this time.
0 commit comments