Skip to content

Commit e3b89c3

Browse files
committed
questions answered and code rewritten
1 parent 8f3d6cf commit e3b89c3

File tree

1 file changed

+18
-2
lines changed
  • Sprint-2/1-key-errors

1 file changed

+18
-2
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// the code will throw an error because the variable str is being declared twice within the same scope
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+
/*
79
function capitalise(str) {
810
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
911
return str;
1012
}
1113
12-
// =============> write your explanation here
13-
// =============> write your new code here
14+
*/
15+
16+
// =============> write your explanation
17+
/* the code throws a SyntaxError because the variable 'str' is declared twice in the same scope using 'let'. In JavaScript,
18+
you cannot declare a variable with the same name more than once in the same scope. The first declaration of 'str' is as a
19+
function parameter, and the second declaration is within the function body. To fix this, you can either rename the second
20+
'str' variable or remove the 'let' keyword from the second declaration to reassign the value to the existing parameter variable.*/
21+
22+
// =============> write your new code here
23+
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)