-
-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy path0.js
More file actions
26 lines (23 loc) · 1.31 KB
/
0.js
File metadata and controls
26 lines (23 loc) · 1.31 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
// Predict and explain first...
// =============> write your prediction here
// The function when called was supposed to capitalise the first letter of a string by calling the first character of the string and then transforming
// to uppercase character and adding it back to the string, but because the variable "str" had already been declared it going to throw a syntaxerror
// 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;
// }
// console.log(capitalise("what is your name?"))
// =============> write your explanation here
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
// ^
// SyntaxError: Identifier 'str' has already been declared
// As predicted,when the function is called, it does not compile and throws a syntax error because the identifier "str" had already been declared as a parameter of the function. This violate the rules of JavaScript
// Variable can be re-assigned using the "let" keyword but cannot be redeclared
// =============> write your new code here
function capitalise(str) {
let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`;
return capitalisedStr;
}
console.log(capitalise("tell me about yourself"));