Skip to content

Commit 3b49e4d

Browse files
authored
Correct variable declaration and return percentage
Fixed variable redeclaration and updated function to return percentage.
1 parent 07e6734 commit 3b49e4d

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

  • Sprint-3/1-key-errors

‎Sprint-3/1-key-errors/1.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5+
// An error will occur because `decimalNumber` is declared twice
6+
// inside the function: once as a parameter and again using `const`.
57

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

@@ -15,6 +17,16 @@ function convertToPercentage(decimalNumber) {
1517
console.log(decimalNumber);
1618

1719
// =============> write your explanation here
20+
// The function already has a parameter called decimalNumber.
21+
// The line const decimalNumber = 0.5 tries to declare another variable with the same name in the same scope.
22+
// JavaScript does not allow this, so a SyntaxError occurs.
23+
// There is also another problem: decimalNumber only exists inside the function, so console.log(decimalNumber) outside the function would cause a ReferenceError.
1824

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

0 commit comments

Comments
 (0)