-
-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy path1.js
More file actions
35 lines (23 loc) · 1.29 KB
/
1.js
File metadata and controls
35 lines (23 loc) · 1.29 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
// Predict and explain first...
// Why will an error occur when this program runs?
// =============> write your prediction here
//1- The function is trying to declare the decimalNumber twice in the perameter and later as a new variable
//2-console.log() is printing a variable in the local scope where it can't be accessed as it is inside the function
// 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;
// }
// =============> write your explanation here
//similar to the previous exercise where the variable decimal Number has already been declared in the parameter
// and already is a local variable which can't be declared again
//As the decimalNumber inside the function we can't print it using the console.log() as it has no power to the local scope
//instead we can delete the second declaration and just leave the parameter as an input for the user which would be nicely work
// 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.5));