Skip to content

Commit 37da202

Browse files
- Refactor capitalise function to remove variable redeclaration and update comments
- predict and explain the outcome of the code
1 parent f2e91c5 commit 37da202

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

  • Sprint-3/1-key-errors

‎Sprint-3/1-key-errors/0.js‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// My prediction is that the first letter of the string will be capitalized at position 0 because arrays start counting at 0 and slice(1) creates a new string slice starting at position '1' because of the template literals.
34

45
// call the function capitalise with a string input
56
// interpret the error message and figure out why an error is occurring
67

7-
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9-
return str;
10-
}
8+
// function capitalise(str) {
9+
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
10+
// return str;
11+
// }
12+
13+
// console.log(capitalise('rahana'));
1114

1215
// =============> write your explanation here
16+
// =============> PREDICTION EXPLANATION: In JavaScript strings are zero-indexed, meaning the first character starts counting at position zero. 'str[0].toUpperCase() aims for the 'r' at position '0' and transforms it to 'R'. The .slice(1) method removes everything from '1' to to the end of the string. By starting at '1', it skips '0' ('r') and gives ('ahana'). the template literals combines 'R' and 'ahana' and adds them together to get('Rahana')
17+
// =============> The SyntaxError: Identifier 'str' has already been declared is occurring because 'str' has already been declared in the function parameter so let has to be named a different. variable(example 'message') so it can return the value of capitalise.
18+
// =============> Alternatively we can remove the let completely and return what is in the template literals.
1319
// =============> write your new code here
20+
21+
// function capitalise(str) {
22+
// let message = `${str[0].toUpperCase()}${str.slice(1)}`;
23+
// return message;
24+
// }
25+
26+
function capitalise(str) {
27+
return `${str[0].toUpperCase()}${str.slice(1)}`;
28+
}
29+
30+
console.log(capitalise('rahana'));

0 commit comments

Comments
 (0)