Skip to content

Commit 6e9aa77

Browse files
committed
update: Fix variable redeclaration error in capitalise function and update comments for clarity Sprint-2/1-key-errors/0.js
1 parent 4c2c13b commit 6e9aa77

File tree

1 file changed

+19
-4
lines changed
  • Sprint-2/1-key-errors

1 file changed

+19
-4
lines changed

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,25 @@
44
// call the function capitalise with a string input
55
// interpret the error message and figure out why an error is occurring
66

7-
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9-
return str;
10-
}
7+
// function capitalise(str) {
8+
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9+
// return str;
10+
// }
1111

1212
// =============> write your explanation here
13+
// The function capitalize is trying to take a string input and return it with the first letter capitalized.
14+
// However, the error occurs because the variable 'str' is being declared twice: once as a parameter and again inside the function.
15+
// This is not allowed in JavaScript, as 'let' does not allow redeclaration of the same variable in the same scope.
16+
// This causes a syntax error because you cannot redeclare a variable with 'let' in the same scope.
1317
// =============> write your new code here
18+
19+
function capitalise(str) {
20+
let name = `${str[0].toUpperCase()}${str.slice(1)}`;
21+
return name;
22+
}
23+
capitalise("hello, check ");
24+
console.log(capitalise("hello, check "));
25+
// =============> write your explanation here
26+
// The function now uses a different variable name 'name' instead of 'str' inside the function.
27+
// This avoids the redeclaration error. The function takes a string input, capitalizes the first letter, and returns the modified string.
28+
// However, the return statement still returns 'str' instead of 'name', which is a mistake.

0 commit comments

Comments
 (0)