Skip to content

Commit d093f6d

Browse files
author
Pretty Taruvinga
committed
fixed ReferenceError
1 parent 3372770 commit d093f6d

File tree

1 file changed

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

1 file changed

+9
-2
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// I predict that the error is occurring because there is a variable name conflict. The parameter 'str' is being redeclared inside the function, which is not allowed in JavaScript. This will cause a syntax error because we cannot declare a variable with the same name as a parameter within the same scope.
33

44
// call the function capitalise with a string input
5+
// capitalise("hello");
6+
57
// interpret the error message and figure out why an error is occurring
68

79
function capitalise(str) {
810
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
911
return str;
1012
}
1113

12-
// =============> write your explanation here
14+
// The error occurs because we are trying to declare a new variable 'str' inside the function, which is already declared as a parameter. In JavaScript, you cannot declare a variable with the same name as a parameter within the same scope. To fix this error, we can simply remove the 'let' keyword and assign the new value to the existing parameter 'str' instead of trying to redeclare it.
15+
16+
function capitalise(str) {
1317
// =============> write your new code here
18+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
19+
return str;
20+
}

0 commit comments

Comments
 (0)