File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11// Predict and explain first...
2+
23// =============> write your prediction here
4+ // str is already a declared variable fed into the capitalise function, the function attempts
5+ // to declare the variable again, this may throw an error.
6+
37
48// call the function capitalise with a string input
59// interpret the error message and figure out why an error is occurring
610
11+
12+ // Error:
13+
14+ // let str = `${str[0].toUpperCase()}${str.slice(1)}`;
15+ // SyntaxError: Identifier 'str' has already been declared
16+
17+
18+ /* Original Code:
19+
720function capitalise(str) {
821 let str = `${str[0].toUpperCase()}${str.slice(1)}`;
922 return str;
10- }
23+ }
24+
25+ */
1126
1227// =============> write your explanation here
28+ // As predicted, an already declared variable cannot be declared again
29+
1330// =============> write your new code here
31+
32+ function capitalise ( str ) {
33+ str = `${ str [ 0 ] . toUpperCase ( ) } ${ str . slice ( 1 ) } ` ;
34+ return str ;
35+ }
36+
Original file line number Diff line number Diff line change 11// Predict and explain first...
2-
32// Why will an error occur when this program runs?
3+
44// =============> write your prediction here
5+ // Assuming we define decimalNumber for the function (otherwise that will throw an error)
6+ // then we run into the same problem as the last exercise, const decimalNumber = 0.5;
7+ // is trying to define a variable that has already been determined
58
69// Try playing computer with the example to work out what is going on
710
11+ /* Original code:
812function convertToPercentage(decimalNumber) {
913 const decimalNumber = 0.5;
1014 const percentage = `${decimalNumber * 100}%`;
@@ -13,8 +17,20 @@ function convertToPercentage(decimalNumber) {
1317}
1418
1519console.log(decimalNumber);
20+ */
1621
1722// =============> write your explanation here
23+ // Uncaught SyntaxError: Identifier 'decimalNumber' has already been declared
24+ // As predicted the function is trying to declare a variable that has already been determined
1825
1926// Finally, correct the code to fix the problem
2027// =============> write your new code here
28+
29+ function convertToPercentage ( decimalNumber ) {
30+ decimalNumber = 0.5 ;
31+ const percentage = `${ decimalNumber * 100 } %` ;
32+
33+ return percentage ;
34+ }
35+
36+ console . log ( decimalNumber ) ;
Original file line number Diff line number Diff line change 44// this function should square any number but instead we're going to get an error
55
66// =============> write your prediction of the error here
7+ // num hasn't been defined, that will throw an error
78
9+ /*
810function square(3) {
9- return num * num ;
11+ return num3 * num3 ;
1012}
13+ */
1114
1215// =============> write the error message here
16+ // Uncaught SyntaxError: Unexpected number
1317
1418// =============> explain this error message here
19+ // the error appears in the line before, a function has to take defined parameters, these
20+ // parameters cannot be represented by numbers (or at least begin with numbers)
1521
1622// Finally, correct the code to fix the problem
1723
1824// =============> write your new code here
1925
26+ function square ( num ) {
27+ return num * num ;
28+ }
29+
2030
You can’t perform that action at this time.
0 commit comments