Skip to content

Commit b573634

Browse files
committed
Fix variable declaration error in capitalise function
1 parent 3372770 commit b573634

File tree

1 file changed

+11
-0
lines changed
  • Sprint-2/1-key-errors

1 file changed

+11
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// I predict there will be a syntax error but not sure what the error is yet. I think it has something to do with the variable declaration of str.
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,15 @@ function capitalise(str) {
910
return str;
1011
}
1112

13+
console.log(capitalise("hello world"));
1214
// =============> write your explanation here
15+
// it showed SyntaxError: Identifier 'str' has already been declared, this is because we have declared the variable str twice,
16+
// once as a parameter and once as a variable inside the function. To fix this error, we can either change the name of the
17+
// variable inside the function to something else like newStr.
1318
// =============> write your new code here
19+
function capitalise(str) {
20+
let newStr = `${str[0].toUpperCase()}${str.slice(1)}`;
21+
return newStr;
22+
}
23+
24+
console.log(capitalise("hello world"));

0 commit comments

Comments
 (0)