-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.js
More file actions
36 lines (30 loc) · 1.37 KB
/
Copy path1.js
File metadata and controls
36 lines (30 loc) · 1.37 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
// Predict and explain first...
// Why will an error occur when this program runs?
// =============> write your prediction here
// 1.*Answer
// We have declared "decimalNumber" twice, which is not allowed.
// It is already declared as a function parameter, causing a conflict.
// console.log(decimalNumber) results in an error because it is outside
// the function scope.It should call console.log(convertToPercentage(decimalNumber));
//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
// 2.*Answer
// It throws an error detailed below;
// /home/justice/Documents/CYF/Module-Structuring-and-Testing-Data/Sprint-2/1-key-errors/1.js:12
// const decimalNumber = 0.5;
// ^
// SyntaxError: Identifier 'decimalNumber' has already been declared
// Removed the const declaration and returned the percentage calculation
// using a template literal to append the percent symbol.
// Finally, correct the code to fix the problem
// =============> write your new code here
function convertToPercentage(decimalNumber) {
return `${decimalNumber * 100}%`;
}
console.log(convertToPercentage(0.5));//returns 50%