-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.js
More file actions
43 lines (27 loc) · 1.42 KB
/
1.js
File metadata and controls
43 lines (27 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Predict and explain first...
// Why will an error occur when this program runs?
// decimalNumber is defined within the function so it will not be recognised in console.log(decimalNumber);
// Try playing computer with the example to work out what is going on
function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;
return percentage;
``}
console.log(decimalNumber);
// this is a function to convert a decimal number to a percentage - function declared
// the decimal number is declared as 0.5
// percentage is calculated by multiplying the decimal number by 100 and then adding the % symbol
// the answer returned is named percentage and gives a number followed by %
// the answer will be printed in console
// Finally, correct the code to fix the problem
// the error was actually 'decimalNumber' has already been declared
// this was because decimalNumber was declared inside the function convertToPercentage(decimalNumber) and
// declared again within the function with the const statement
// once const decimalNumber = 0.5; was moved outside the function this error was resolved
// that way the global decimalNumber exists and the function can still accept it as an argument
// const decimalNumber = 0.5;
// function convertToPercentage(decimalNumber) {
// const percentage = `${decimalNumber * 100}%`;
// return percentage;
}
// console.log(decimalNumber);