File tree Expand file tree Collapse file tree 1 file changed +16
-0
lines changed
Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Original file line number Diff line number Diff line change 11// Predict and explain first...
22// =============> write your prediction here
3+ // Prediction: The code will throw a SyntaxError because the variable 'str' has already been declared as a parameter of the function.
34
45// call the function capitalise with a string input
56// interpret the error message and figure out why an error is occurring
67
8+ // Original code with error:
9+
10+ /*
711function capitalise(str) {
812 let str = `${str[0].toUpperCase()}${str.slice(1)}`;
913 return str;
1014}
15+ */
1116
1217// =============> write your explanation here
18+ // Explanation: The error occurs because the variable 'str' is being redeclared within the function using 'let',
19+ // which is not allowed since 'str' is already declared as a parameter of the function.
20+ // To fix this, we can either rename the inner variable or simply assign the new value to the existing parameter without redeclaring it.
21+
1322// =============> write your new code here
23+ // Fixed code:
24+ function capitalise ( str ) {
25+ str = `${ str [ 0 ] . toUpperCase ( ) } ${ str . slice ( 1 ) } ` ;
26+ return str ;
27+ }
28+
29+ console . log ( capitalise ( "hello" ) ) ; // Output: "Hello"
You can’t perform that action at this time.
0 commit comments