|
1 | 1 | // Predict and explain first... |
2 | 2 | // =============> write your prediction here |
3 | 3 |
|
| 4 | +// Predict and explain first... |
| 5 | +// =============> write your prediction here |
| 6 | +// This code will throw an error: "SyntaxError: Identifier 'str' has already been declared". |
| 7 | +// The reason is that the variable 'str' is declared twice — once as a function parameter |
| 8 | +// and again with `let str` inside the function. You cannot redeclare a parameter variable |
| 9 | +// using `let` or `const` in the same scope. |
| 10 | + |
4 | 11 | // call the function capitalise with a string input |
5 | 12 | // interpret the error message and figure out why an error is occurring |
6 | 13 |
|
| 14 | +// function capitalise(str) { |
| 15 | +// let str = `${str[0].toUpperCase()}${str.slice(1)}`; |
| 16 | +// return str; |
| 17 | +// } |
| 18 | + |
| 19 | +// =============> write your explanation here |
| 20 | + |
| 21 | +// The error occurs because 'str' is already defined as a parameter to the function. |
| 22 | +// Using 'let str' again inside the function tries to redeclare it, which is not allowed. |
| 23 | +// We can fix this by either using a different variable name or by reassigning 'str' |
| 24 | +// directly without redeclaring it. |
| 25 | + |
| 26 | +// =============> write your new code here |
7 | 27 | function capitalise(str) { |
8 | | - let str = `${str[0].toUpperCase()}${str.slice(1)}`; |
| 28 | + str = `${str[0].toUpperCase()}${str.slice(1)}`; |
9 | 29 | return str; |
10 | 30 | } |
11 | 31 |
|
12 | | -// =============> write your explanation here |
13 | | -// =============> write your new code here |
| 32 | +// Example: |
| 33 | +console.log(capitalise("hello")); // Output: "Hello" |
| 34 | + |
0 commit comments