Skip to content

Commit 27843a8

Browse files
committed
fixed code and added explanation
1 parent 67c3257 commit 27843a8

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

  • Sprint-3/1-key-errors

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22
//This code will throw an error as we haven't passed an argument into str? or str is the function parameter and variable already on line 5 so it may throw an error to declare again as a variable inside the function?
33
// call the function capitalise with a string input
44
// interpret the error message and figure out why an error is occurring
5+
//function capitalise(str) {
6+
//let str = `${str[0].toUpperCase()}${str.slice(1)}`;
7+
// return str;
8+
//}
9+
//console.log(capitalise("mars"));
10+
//SyntaxError: Identifier 'str' has already been declared
11+
// This code threw an error as "str" has already been defined in the function capitalise(str) so let str is causing the error as it cannot be defined twice.
512
function capitalise(str) {
6-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
7-
return str;
13+
let result = `${str[0].toUpperCase()}${str.slice(1)}`;
14+
return result;
815
}
9-
// =============> write your explanation here
10-
// =============> write your new code here
16+
console.log(capitalise("mars"));
17+
// In my new code i'm calling the function capitalise(str) in my code "str" is only the parameter and not the variable aswell. In the variable result it will take any argument and using the toUpperCase method itll make a copy of the the letter at index zero as a capital letter so as i passed in "mars" itll return M and store it in the variable result. The method slice will return a copy of the argument from index 1 onwards so "ars" and because it's in a template literal it will join "M" and "ars" together and this is what will get stored in result and by returning result on line 14 i'm asking the function to return what is stored in result so javascript can print it using console.log or i can use it elsewhere as i'm coding.

0 commit comments

Comments
 (0)