Skip to content

Commit 46ad0c8

Browse files
committed
error predicted, code rewritten and explained
1 parent 6b9b495 commit 46ad0c8

File tree

1 file changed

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

1 file changed

+12
-0
lines changed

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

Lines changed: 12 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+
// The variable str is being declared twice within the same scope, which will cause a syntax error.
34

45
// call the function capitalise with a string input
56
// interpret the error message and figure out why an error is occurring
@@ -8,6 +9,17 @@ function capitalise(str) {
89
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
910
return str;
1011
}
12+
const result = capitalise('hello');
13+
console.log(result);
14+
// SyntaxError: Identifier 'str' has already been declared
1115

1216
// =============> write your explanation here
17+
// // The first declaration is in the function parameter and the second one is inside the function body.
18+
// In JavaScript, you cannot declare the same variable name in the same scope using let or const.
19+
// To fix this, we can either rename the inner variable or assign the capitalised value to the parameter itself without redeclaring it.
20+
1321
// =============> write your new code here
22+
function capitalise(str) {
23+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
24+
return str;
25+
}

0 commit comments

Comments
 (0)