-
-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy path1.js
More file actions
35 lines (24 loc) · 1.31 KB
/
1.js
File metadata and controls
35 lines (24 loc) · 1.31 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
// I predict that the decimalNumber variable will be timed 100 and will get a percentage value of 50%, but that only my predictions.
// 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
// SyntaxError: Identifier 'decimalNumber' has already been declared
// This error occurs because we have declared the variable decimalNumber twice,
// once as a parameter and once as a variable inside the function.
// apparently there is another error that we only console logged the variable decimalNumber which is not declared in the global scope,
// to fix this we instead call the function convertToPercentage with a decimal number as an argument and log the result of that function call to the console.
// 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));