You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 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.
3
5
4
6
// call the function capitalise with a string input
5
7
// 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"
6
9
7
10
functioncapitalise(str){
8
11
letstr=`${str[0].toUpperCase()}${str.slice(1)}`;
9
12
returnstr;
10
13
}
11
-
14
+
console.log(capitalise("hello"));
12
15
// =============> 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.
13
19
// =============> write your new code here
20
+
21
+
functioncapitalize(str){
22
+
str=`${str[0].toUpperCase()}${str.slice(1)}`;
23
+
returnstr;
24
+
}
25
+
console.log(capitalize("the first letter should be capitalized"));
0 commit comments