1- function capitaliseFirstLetter ( string ) {
2- string = `${ string [ 0 ] . toUpperCase ( ) } ` ;
3- return string ;
1+ // Predict and explain first...
2+ // the code is supposed to convert a decimal number to percentage by multiplying
3+ // the decimal Number by 100 then add the sign % to show that it is a percentage
4+
5+ // Why will an error occur when this program runs?
6+ // =============> write your prediction here
7+ // an error will occur because the variable decimalNumber is redeclared inside the function.
8+ // it's already declared as a parameter of the function, so redeclaring it with const causes a syntax error.
9+
10+ // Try playing computer with the example to work out what is going on
11+
12+ function convertToPercentage ( decimalNumber ) {
13+ const decimalNumber = 0.5 ;
14+ const percentage = `${ decimalNumber * 100 } %` ;
15+
16+ return percentage ;
17+ }
18+
19+ console . log ( decimalNumber ) ;
20+
21+ // =============> write your explanation here
22+ // the fucntion convertToPercentage has a parameter named decimalNumber,
23+ // but inside the function, there is a line that tries to declare a new constant with the same name decimalNumber.
24+ // there is no need to redeclare the variable or edit it since it is a user input.
25+ // also the variable decimalNumber is called from outside the function which menas from another scope.
26+ // that is wrong because the variable can only be used inside the fucntion. instead of that we need to call the funtion, not hte variable.
27+
28+ // Finally, correct the code to fix the problem
29+ // =============> write your new code here
30+
31+ function convertToPercentage ( decimalNumber ) {
32+ const percentage = `${ decimalNumber * 100 } %` ;
33+ return percentage ;
434}
535
6- console . log ( capitaliseFirstLetter ( "hello" ) ) ;
36+ console . log ( convertToPercentage ( 0.3 ) ) ;
0 commit comments