-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0.js
More file actions
29 lines (24 loc) · 1.2 KB
/
Copy path0.js
File metadata and controls
29 lines (24 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Predict and explain first...
// =============> write your prediction here
// Answer
// If I call the function capitalise with a string input, I predict that it will return an error because the variable str
// has already been declared as a paramter of the function so it can not be re-declared
// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
//function capitalise(str) {
//let str = `${str[0].toUpperCase()}${str.slice(1)}`;
//return str;
//}
// =============> write your explanation here
//Answer
// 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.
// 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.
// Redeclaring the same variable within the same function or block scope using let is not allowed in JavaScript.
// =============> write your new code here
// Answer
function capitalise(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
let actualOutput = capitalise("welcome");
console.log(actualOutput);