File tree Expand file tree Collapse file tree 1 file changed +18
-2
lines changed
Expand file tree Collapse file tree 1 file changed +18
-2
lines changed Original file line number Diff line number Diff line change 11// Predict and explain first...
22// =============> write your prediction here
3+ // the code will throw an error because the variable str is being declared twice within the same scope
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+ /*
79function capitalise(str) {
810 let str = `${str[0].toUpperCase()}${str.slice(1)}`;
911 return str;
1012}
1113
12- // =============> write your explanation here
13- // =============> write your new code here
14+ */
15+
16+ // =============> write your explanation
17+ /* the code throws a SyntaxError because the variable 'str' is declared twice in the same scope using 'let'. In JavaScript,
18+ you cannot declare a variable with the same name more than once in the same scope. The first declaration of 'str' is as a
19+ function parameter, and the second declaration is within the function body. To fix this, you can either rename the second
20+ 'str' variable or remove the 'let' keyword from the second declaration to reassign the value to the existing parameter variable.*/
21+
22+ // =============> write your new code here
23+
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