-
-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy path1.js
More file actions
44 lines (33 loc) · 1.47 KB
/
1.js
File metadata and controls
44 lines (33 loc) · 1.47 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
43
44
// Predict and explain first...
// Why will an error occur when this program runs?
// =============> write your prediction here
// Prediction:
// An error will occur because `decimalNumber` is declared twice.
// It is first declared as a function parameter and then declared again
// inside the function using `const decimalNumber = 0.5;`.
// JavaScript does not allow redeclaring the same variable in the same scope.
// Also, `console.log(decimalNumber);` is outside the function,
// so `decimalNumber` is not defined in the global scope.
// 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
// Explanation:
// The parameter `decimalNumber` is already defined in the function.
// Declaring `const decimalNumber = 0.5;` again causes a SyntaxError:
// "Identifier 'decimalNumber' has already been declared".
// Additionally, `console.log(decimalNumber);` causes a ReferenceError
// because `decimalNumber` only exists inside the function and
// cannot be accessed outside of it.
// Finally, correct the code to fix the problem
// =============> write your new code here
function convertToPercentage(decimalNumber) {
const percentage = `${decimalNumber * 100}%`;
return percentage;
}
const result = convertToPercentage(0.5);
console.log(result); // 50%