File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 11// Predict and explain first...
22// =============> write your prediction here
33
4+ /** Prediction:
5+ * The code will throw an error because the parameter str and the variable str inside the function are both declared in the same scope.
6+ *
7+ */
8+
49// call the function capitalise with a string input
510// interpret the error message and figure out why an error is occurring
611
12+ /** Original function:
13+ *
714function capitalise(str) {
815 let str = `${str[0].toUpperCase()}${str.slice(1)}`;
916 return str;
1017}
18+ */
1119
1220// =============> write your explanation here
21+ /** Explanation:
22+ * The error occurs because:
23+ *
24+ * The function parameter str is already declared in the function scope
25+ * Inside the function, we're trying to redeclare str using let which creates a new variable in the same scope
26+ * JavaScript doesn't allow redeclaring a variable with let in the same scope
27+ * The error message would be something like:
28+ * SyntaxError: Identifier 'str' has already been declared
29+ */
30+
1331// =============> write your new code here
32+
33+ function capitalise ( str ) {
34+ str = `${ str [ 0 ] . toUpperCase ( ) } ${ str . slice ( 1 ) } ` ;
35+ return str ;
36+ }
37+
38+ // Test the function
39+ console . log ( capitalise ( "hello" ) ) ; // "Hello"
40+ console . log ( capitalise ( "world" ) ) ; // "World"
41+
You can’t perform that action at this time.
0 commit comments