Skip to content

Commit 2a18e2c

Browse files
explained in comments why errors occured and then refactored the code to fix the convert to percentage function works correctly
1 parent 832d9bb commit 2a18e2c

File tree

1 file changed

+14
-1
lines changed
  • Sprint-2/1-key-errors

1 file changed

+14
-1
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,32 @@
22

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5+
// I expect a Syntax error to be thrown due to the const "decimalNumber" has already been declaired in the parameters of the function.
6+
// I also expect an error from the "console.log(decimalnumber)" as its calling "decimalNumber" from outside the function
57

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

10+
/*
811
function convertToPercentage(decimalNumber) {
912
const decimalNumber = 0.5;
1013
const percentage = `${decimalNumber * 100}%`;
1114
1215
return percentage;
1316
}
1417
15-
console.log(decimalNumber);
18+
console.log(decimalNumber);
19+
*/
1620

1721
// =============> write your explanation here
22+
// When running the code i get thrown a Syntax error: "identifer 'decimalNumber' has already been declared". To solve this you must remove the "const decimalNumber = 0.5" from the function as its already been defined in the parameters of the function.
23+
// after testing the code and fixing the syntax error we then recived another error: "ReferenceError: decimalNumber is not defined" To solve this you must change the "console.log(decimalNumber)" to "console.log(convertToPercentage())".
1824

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

0 commit comments

Comments
 (0)