Skip to content

Commit 07e6734

Browse files
authored
Fix redeclaration error in capitalise function
Remove redeclaration of 'str' in capitalise function.
1 parent f5b9e81 commit 07e6734

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

  • Sprint-3/1-key-errors

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// I predict this code will produce a SyntaxError because str is declared twice in the same scope using let.
34

45
// call the function capitalise with a string input
6+
capitalise("hello");
57
// interpret the error message and figure out why an error is occurring
68

79
function capitalise(str) {
@@ -10,4 +12,14 @@ function capitalise(str) {
1012
}
1113

1214
// =============> write your explanation here
15+
// The error occurs because str is already declared as a parameter
16+
// Inside the function, let str attempts to declare another variable
17+
// with the same name in the same scope.
18+
// We can fix the problem by removing let and assigning the new value
19+
// directly to the existing parameter.
20+
1321
// =============> write your new code here
22+
function capitalise(str) {
23+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
24+
return str;
25+
}

0 commit comments

Comments
 (0)