Skip to content

Commit 0224852

Browse files
committed
key errors completed
1 parent 124ae45 commit 0224852

File tree

1 file changed

+17
-4
lines changed
  • Sprint-2/1-key-errors

1 file changed

+17
-4
lines changed

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
// Predict and explain first...
22
// =============> write your prediction here
33

4+
// The code will throw an error because the variable "str" is being declared twice in the same scope.
5+
46
// call the function capitalise with a string input
57
// interpret the error message and figure out why an error is occurring
68

7-
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9-
return str;
10-
}
9+
//Original code with errors:
10+
11+
// function capitalise(str) {
12+
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
13+
// return str;
14+
// }
1115

1216
// =============> write your explanation here
17+
// The error occurs because the variable "str" is being declared twice in the same scope. This causes a conflict and results in a syntax error.
18+
// To fix this error, we can simply remove the "let" keyword when reassigning the value to "str" inside the function. This way we are not declaring a new variable, but rather reassigning the existing parameter "str".
19+
1320
// =============> write your new code here
21+
// Updated Code:
22+
function capitalise(str) {
23+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
24+
return str;
25+
}
26+
console.log(capitalise("hello"));

0 commit comments

Comments
 (0)