-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.js
More file actions
27 lines (20 loc) · 1.2 KB
/
Copy path1.js
File metadata and controls
27 lines (20 loc) · 1.2 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
// Predict and explain first...
// =====> This function tries to convert a decimal number to percentage, but I think it will not run as we have declared same variable name two times.
// Why will an error occur when this program runs?
// =============> It will throw an error as we have declared decimalNumber variable in our function parameter and again we declared this decimalNumber variable inside our 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;
}
console.log(decimalNumber);*/
// =============> I tried to run this, it throw an error like: 'decimalNumber' has already been declared. to fix this we should remove the decimalNumber variable which we have inside our function.
// 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.9));
// I have removed the log inside the function, as we did not need to print that message each time we call the function.