Skip to content

Commit 09a867e

Browse files
Fix the error and explain
1 parent 73c30ee commit 09a867e

File tree

1 file changed

+20
-3
lines changed
  • Sprint-2/1-key-errors

1 file changed

+20
-3
lines changed

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
1-
// Predict and explain first...
1+
// Predict and explain first...
2+
//Syntax error
23
// =============> write your prediction here
4+
// We pass str as a parameter to the function.
5+
//The parameter becomes a local variable inside the function scope.
6+
//Therefore, we cannot declare another variable with the same name inside the same scope.
37

48
// call the function capitalise with a string input
59
// interpret the error message and figure out why an error is occurring
610

711
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
12+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
913
return str;
1014
}
11-
15+
let result = capitalise("Arun");
16+
console.log(result);
1217
// =============> write your explanation here
18+
// The parameter str is already a local variable inside the function.
19+
// Previously, redeclaring it using let caused an error because variables cannot be declared twice in the same scope.
20+
// Instead of redeclaring it, I modified the existing str variable and returned it.
21+
// This avoids the redeclaration error and the code runs correctly.
1322
// =============> write your new code here
23+
function capitalise(str) {
24+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
25+
return str;
26+
}
27+
let str = capitalise("Arun");
28+
console.log(str);
29+
30+

0 commit comments

Comments
 (0)