File tree Expand file tree Collapse file tree 3 files changed +43
-23
lines changed
Expand file tree Collapse file tree 3 files changed +43
-23
lines changed Original file line number Diff line number Diff line change 11// Predict and explain first...
2- // =============> write your prediction here
2+ // =============> I predict it will throw a syntaxerror because the parameter 'str' is already declare in the definition,
3+ //and line 9 try to declare another variable with the same name so the error will be identifier 'str'has already been declared .
4+
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- }
9+ // function capitalise(str) {
10+ // let str = `${str[0].toUpperCase()}${str.slice(1)}`;
11+ // return str;
12+ // }
13+ // capitalise("ahmadhmedan");
1114
12- // =============> write your explanation here
13- // =============> write your new code here
15+ // =============> As I predict it will throw a SyntaxError: Identifier 'str' has already been declared
16+ //because in JavaScript we can not redeclare the same variable in the same scope.
17+ function Capitalise ( str ) {
18+ return `${ str [ 0 ] . toUpperCase ( ) } ${ str . slice ( 1 ) } ` ;
19+ }
20+ console . log ( Capitalise ( "ahmadHmedan" ) ) ;
Original file line number Diff line number Diff line change 22
33// Why will an error occur when this program runs?
44// =============> write your prediction here
5-
5+ // my prediction it will throw a syntaxerror because the parameter decimalNumber is already declare in the definition.
6+ //and line 11 try to declare another variable with the same name so the error will be identifier 'str'has already been declared .
7+ //
68// Try playing computer with the example to work out what is going on
79
8- function convertToPercentage ( decimalNumber ) {
9- const decimalNumber = 0.5 ;
10- const percentage = `${ decimalNumber * 100 } %` ;
10+ // function convertToPercentage(decimalNumber) {
11+ // const decimalNumber = 0.5;
12+ // const percentage = `${decimalNumber * 100}%`;
1113
12- return percentage ;
13- }
14+ // return percentage;
15+ // }
1416
15- console . log ( decimalNumber ) ;
17+ // console.log(decimalNumber);
1618
17- // =============> write your explanation here
19+ // =============> As I predicted, SyntaxError: Identifier 'decimalNumber' has already been declared
1820
1921// Finally, correct the code to fix the problem
2022// =============> write your new code here
23+ function convertToPercentage ( decimalNumber ) {
24+
25+ const percentage = `${ decimalNumber * 100 } %` ;
26+
27+ return percentage ;
28+ }
29+
30+ console . log ( convertToPercentage ( 0.5 ) ) ;
Original file line number Diff line number Diff line change 33
44// this function should square any number but instead we're going to get an error
55
6- // =============> write your prediction of the error here
6+ // =============> // It will throw a syntaxerror because in the function definition we sit the parameter name not the value
77
8- function square ( 3 ) {
9- return num * num ;
10- }
8+ // function square(3) {
9+ // return num * num;
10+ // }
1111
12- // =============> write the error message here
12+ // =============> SyntaxError: Unexpected number
1313
14- // =============> explain this error message here
14+ // =============> As I predicted we must set the parameter name in the definition
1515
1616// Finally, correct the code to fix the problem
1717
18- // =============> write your new code here
19-
18+ // =============>
19+ function square ( num ) {
20+ return num * num ;
21+ }
2022
23+ console . log ( square ( 3 ) ) ;
You can’t perform that action at this time.
0 commit comments