Skip to content

Commit d957dd1

Browse files
committed
Avoid redeclaring 'str' inside capitalise function
1 parent 0748f7c commit d957dd1

File tree

1 file changed

+15
-1
lines changed
  • Sprint-2/1-key-errors

1 file changed

+15
-1
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// =============> // =============> write your prediction here
3+
// I predict the code will throw an error because the variable `str` is being declared twice in the same function scope.
4+
35

46
// call the function capitalise with a string input
57
// interpret the error message and figure out why an error is occurring
@@ -10,4 +12,16 @@ function capitalise(str) {
1012
}
1113

1214
// =============> write your explanation here
15+
// The function already receives `str` as a parameter.
16+
// Inside the function, it tries to declare `let str = ...`,
17+
// which causes a conflict because JavaScript does not
18+
// allow redeclaring the same variable name in the same scope using `let`.
19+
// This leads to a `SyntaxError: Identifier 'str' has already been declared`.
20+
21+
1322
// =============> write your new code here
23+
// =============> write your new code here
24+
function capitalise(str) {
25+
let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`;
26+
return capitalisedStr;
27+
}

0 commit comments

Comments
 (0)