-
-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy path1.js
More file actions
39 lines (28 loc) · 1.23 KB
/
1.js
File metadata and controls
39 lines (28 loc) · 1.23 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
// Predict and explain first...
// Why will an error occur when this program runs?
// =============> write your prediction here
// When the JS executed the code, it will give 2 possible error 1 is decimalNumber is define inside the function and decimalNumber is undefined when using console.log
// 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);
*/
// =============> write your explanation here
/*
For function convertToPercentage there are 1 error:
inside the function the decimalNumber parameter is being re-declare and given a fix value, this will give this function a
decimalNumber are ready defined.
Next is when we console.log the function instead of calling the function name and value we used the function parameter variable name
instead. This will give the error decimalNumber is undefined.
*/
// Finally, correct the code to fix the problem
// =============> write your new code here
function convertToPercentage(decimalNumber) {
const percentage = `${decimalNumber * 100}%`;
return percentage;
}
console.log(convertToPercentage(0.25));