Skip to content

Commit 4cb9354

Browse files
fix: resolve variable redeclaration error in capitalize function
1 parent 4c2c13b commit 4cb9354

File tree

1 file changed

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

1 file changed

+13
-1
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// I expect the code to possibly throw an error because str is being declared twice. once in the function and again in the function body declared as "let"
4+
// This function looks like its trying to turn the first letter of a string into a capital letter.
35

46
// call the function capitalise with a string input
57
// interpret the error message and figure out why an error is occurring
8+
// after trying to run the code it produced a "SyntaxError: Identifier 'str' has already been declared"
69

710
function capitalise(str) {
811
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
912
return str;
1013
}
11-
14+
console.log(capitalise("hello"));
1215
// =============> write your explanation here
16+
// this is because the variable str is declared twice, once in the function parameters and again inside the function body with "let str"
17+
// str is declared as a parameter, so we don't need to declare it again with let you can just use it directly.
18+
// to fix the error, we can remove the "let" keyword from the second declaration of str.
1319
// =============> write your new code here
20+
21+
function capitalize(str) {
22+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
23+
return str;
24+
}
25+
console.log(capitalize("the first letter should be capitalized"));

0 commit comments

Comments
 (0)