Skip to content

Commit b61cf21

Browse files
committed
predicted the output and explained the error in 0.js file
1 parent 3372770 commit b61cf21

File tree

1 file changed

+18
-5
lines changed
  • Sprint-2/1-key-errors

1 file changed

+18
-5
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// 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
4+
// 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
35

46
// call the function capitalise with a string input
57
// interpret the error message and figure out why an error is occurring
68

7-
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9-
return str;
10-
}
11-
9+
// function capitalise(str) {
10+
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
11+
// return str;
12+
// }
13+
// console.log(capitalise("what is your name?"))
1214
// =============> write your explanation here
15+
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
16+
// ^
17+
18+
// SyntaxError: Identifier 'str' has already been declared
19+
// 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
20+
// Variable can be re-assigned using the "let" keyword but cannot be redeclared
1321
// =============> write your new code here
22+
function capitalise(str) {
23+
let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`;
24+
return capitalisedStr;
25+
}
26+
console.log(capitalise("tell me about yourself"))

0 commit comments

Comments
 (0)