Skip to content

Commit 7993291

Browse files
committed
Predict the output of the provided function, explain the actual error, and write the correct code.
1 parent 1f028f7 commit 7993291

File tree

1 file changed

+19
-3
lines changed
  • Sprint-2/1-key-errors

1 file changed

+19
-3
lines changed

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// Answer
4+
// If I call the function capitalise with a string input, I predict that it will return an error because the variable str
5+
// has already been declared as a paramter of the function so it can not be re-declared
36

47
// call the function capitalise with a string input
58
// interpret the error message and figure out why an error is occurring
69

10+
//function capitalise(str) {
11+
//let str = `${str[0].toUpperCase()}${str.slice(1)}`;
12+
//return str;
13+
//}
14+
15+
// =============> write your explanation here
16+
//Answer
17+
// The error message "SyntaxError: Identifier 'str' has already been declared" means that the identifier "str" has been declared and so can not be re-declared.
18+
// This error occured because the same variable name occurs as a function parameter and is then redeclared using a let assignment in a function body again.
19+
// Redeclaring the same variable within the same function or block scope using let is not allowed in JavaScript.
20+
21+
// =============> write your new code here
22+
// Answer
723
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
24+
str = `${str[0].toUpperCase()}${str.slice(1)}`;
925
return str;
1026
}
1127

12-
// =============> write your explanation here
13-
// =============> write your new code here
28+
let actualOutput = capitalise("welcome");
29+
console.log(actualOutput);

0 commit comments

Comments
 (0)