Skip to content

Commit 2fa497c

Browse files
committed
0.js, 1-key-errors, sprint-2 completed
1 parent 530060a commit 2fa497c

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

  • Sprint-2/1-key-errors

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// =============> write your prediction here
3+
// it will throw an error because we are trying to declare a variable with the same name as the function parameter, which is not allowed in JavaScript. The error message will likely indicate that there is a syntax error or that the variable has already been declared.
34

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

7-
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9-
return str;
10-
}
8+
//function capitalise(str) {
9+
//let str = `${str[0].toUpperCase()}${str.slice(1)}`;
10+
//return str;
11+
//}
1112

13+
//capitalise("hello world");
14+
//console.log(capitalise("hello world"));
1215
// =============> write your explanation here
16+
// The error occurs because we are trying to declare a variable named 'str' inside the function, which is the same name as the function parameter. In JavaScript, you cannot declare a variable with the same name as a parameter within the same scope. This results in a syntax error, as the JavaScript engine does not know how to handle the duplicate declaration of 'str'. To fix this issue, we can simply remove the 'let' keyword and assign the new value to 'str' directly, since it is already declared as a parameter.
17+
18+
1319
// =============> write your new code here
20+
function capitalise(str) {
21+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
22+
return str;
23+
}
24+
console.log(capitalise("hello world"));

0 commit comments

Comments
 (0)