Skip to content

Commit 102a561

Browse files
committed
Fixed convertToPercentage function
1 parent 79b6095 commit 102a561

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

  • Sprint-2/1-key-errors

Sprint-2/1-key-errors/1.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,29 @@
22

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5+
/*When this program runs, there will be two errors: the function redeclares decimalNumber,
6+
and decimalNumber is used in console.log even though it is not defined outside the function.*/
57

68
// Try playing computer with the example to work out what is going on
79

8-
function convertToPercentage(decimalNumber) {
10+
/*function convertToPercentage(decimalNumber) {
911
const decimalNumber = 0.5;
1012
const percentage = `${decimalNumber * 100}%`;
1113
1214
return percentage;
1315
}
1416
15-
console.log(decimalNumber);
17+
console.log(decimalNumber);*/
1618

1719
// =============> write your explanation here
20+
// When I run this code, I get a SyntaxError because decimalNumber is already declared inside the function.
1821

1922
// Finally, correct the code to fix the problem
2023
// =============> write your new code here
24+
25+
function convertToPercentage(decimalNumber) {
26+
const percentage = `${decimalNumber * 100}%`;
27+
return percentage;
28+
}
29+
console.log(convertToPercentage(0.5));
30+
console.log(convertToPercentage(0.45));

0 commit comments

Comments
 (0)