Skip to content

Commit f2a2aba

Browse files
committed
Fixed capitaliseStr function and confirmed output with test
1 parent 124ae45 commit f2a2aba

File tree

1 file changed

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

1 file changed

+16
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// Prediction: The code will throw a SyntaxError because the variable 'str' has already been declared as a parameter of the function.
34

45
// call the function capitalise with a string input
56
// interpret the error message and figure out why an error is occurring
67

8+
// Original code with error:
9+
10+
/*
711
function capitalise(str) {
812
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
913
return str;
1014
}
15+
*/
1116

1217
// =============> write your explanation here
18+
// Explanation: The error occurs because the variable 'str' is being redeclared within the function using 'let',
19+
// which is not allowed since 'str' is already declared as a parameter of the function.
20+
// To fix this, we can either rename the inner variable or simply assign the new value to the existing parameter without redeclaring it.
21+
1322
// =============> write your new code here
23+
// Fixed code:
24+
function capitalise(str) {
25+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
26+
return str;
27+
}
28+
29+
console.log(capitalise("hello")); // Output: "Hello"

0 commit comments

Comments
 (0)