-
-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy path1.js
More file actions
41 lines (27 loc) · 1.17 KB
/
1.js
File metadata and controls
41 lines (27 loc) · 1.17 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
// Predict and explain first...
const { t } = require("tar");
// Why will an error occur when this program runs?
// =============> write your prediction here
// 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
// This function will throw a SyntaxError because decimalNumber
// is being redeclared inside the same scope as the parameter.
// Additionally, the function ignores the input parameter
// because the value is hardcoded inside the body.
// Finally, the console.log will throw a ReferenceError
// because it tries to access a local variable from the global scope.
// To fix this, we must remove the internal declaration to use the
// parameter dynamically and invoke the function correctly.
// Finally, correct the code to fix the problem
// =============> write your new code here
function convertToPercentage(num) {
const percentage = `${num * 100}%`;
return percentage;
}
console.log(convertToPercentage(0.80));