Skip to content

Commit 4818ddd

Browse files
committed
Correct a function with let defined inside it
1 parent 269ce23 commit 4818ddd

File tree

1 file changed

+24
-3
lines changed
  • Sprint-2/1-key-errors

1 file changed

+24
-3
lines changed

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

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

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+
411
// call the function capitalise with a string input
512
// interpret the error message and figure out why an error is occurring
613

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
727
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
28+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
929
return str;
1030
}
1131

12-
// =============> write your explanation here
13-
// =============> write your new code here
32+
// Example:
33+
console.log(capitalise("hello")); // Output: "Hello"
34+

0 commit comments

Comments
 (0)