Skip to content

Commit 9b03bec

Browse files
committed
0.js committed
1 parent 3372770 commit 9b03bec

File tree

1 file changed

+28
-0
lines changed
  • Sprint-2/1-key-errors

1 file changed

+28
-0
lines changed

Sprint-2/1-key-errors/0.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,41 @@
11
// Predict and explain first...
22
// =============> write your prediction here
33

4+
/** Prediction:
5+
* The code will throw an error because the parameter str and the variable str inside the function are both declared in the same scope.
6+
*
7+
*/
8+
49
// call the function capitalise with a string input
510
// interpret the error message and figure out why an error is occurring
611

12+
/** Original function:
13+
*
714
function capitalise(str) {
815
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
916
return str;
1017
}
18+
*/
1119

1220
// =============> write your explanation here
21+
/** Explanation:
22+
* The error occurs because:
23+
*
24+
* The function parameter str is already declared in the function scope
25+
* Inside the function, we're trying to redeclare str using let which creates a new variable in the same scope
26+
* JavaScript doesn't allow redeclaring a variable with let in the same scope
27+
* The error message would be something like:
28+
* SyntaxError: Identifier 'str' has already been declared
29+
*/
30+
1331
// =============> write your new code here
32+
33+
function capitalise(str) {
34+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
35+
return str;
36+
}
37+
38+
// Test the function
39+
console.log(capitalise("hello")); // "Hello"
40+
console.log(capitalise("world")); // "World"
41+

0 commit comments

Comments
 (0)