Skip to content

Commit b86d1d1

Browse files
Fix variable redeclaration error in capitalise function and update comments for clarity
1 parent 3372770 commit b86d1d1

File tree

1 file changed

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

1 file changed

+22
-1
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// =============> The code will throw an error because the variable str is a reserved keyword in JavaScript and cannot be used as a variable name.
3+
// The error message will likely indicate that there is a syntax error or that the variable name is invalid.
34

45
// call the function capitalise with a string input
56
// interpret the error message and figure out why an error is occurring
@@ -9,5 +10,25 @@ function capitalise(str) {
910
return str;
1011
}
1112

13+
console.log(str);
14+
1215
// =============> write your explanation here
16+
17+
// C:\Users\alexo\CYF\Module-Structuring-and-Testing-Data\Module-Structuring-and-Testing-Data\Sprint-2\1-key-errors\0.js:8
18+
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
19+
// ^
20+
21+
// SyntaxError: Identifier 'str' has already been declared
22+
23+
// The error message indicates that there is a syntax error in the code because the variable name 'str' has already been declared.
24+
25+
// In the function capitalise, the parameter 'str' is declared as a variable name, and then within the function body, there is another declaration of 'str' using the let keyword.
26+
// This creates a conflict because 'str' is already declared as a parameter, and it cannot be redeclared as a variable within the same scope.
27+
1328
// =============> write your new code here
29+
function capitalise(str) {
30+
let capitalised = `${str[0].toUpperCase()}${str.slice(1)}`;
31+
return capitalised;
32+
}
33+
34+
console.log(capitalise("alex"));

0 commit comments

Comments
 (0)