Skip to content

Commit 2a3a27d

Browse files
committed
Fix variable redeclaration error in capitalise function
1 parent 4c29df2 commit 2a3a27d

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

  • Sprint-3/1-key-errors

‎Sprint-3/1-key-errors/0.js‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// I am predicting the code will generate error because the variable str is declared twice
33

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) {
7+
/*function capitalise(str) {
88
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
99
return str;
1010
}
11+
console.log(capitalise("hello World"));*/
12+
13+
// After we run the code, it generated this error SyntaxError: Identifier str has already been declared
14+
// This happened because we tried to declare again using the keyword let the variable str
15+
// which was declared before in the parameter of the function
1116

12-
// =============> write your explanation here
13-
// =============> write your new code here
17+
//Here is the fixed code
18+
function capitalise(str) {
19+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
20+
return str;
21+
}
22+
console.log(capitalise("hello World"));

0 commit comments

Comments
 (0)