Skip to content

Commit 11b3da1

Browse files
moved const outside the function
1 parent ca7fa61 commit 11b3da1

File tree

1 file changed

+27
-4
lines changed
  • Sprint-2/1-key-errors

1 file changed

+27
-4
lines changed

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,43 @@
11
// Predict and explain first...
22

33
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
4+
// decimalNumber is defined within the function so it will not be recognised in console.log(decimalNumber);
55

66
// Try playing computer with the example to work out what is going on
77

8+
9+
10+
811
function convertToPercentage(decimalNumber) {
912
const decimalNumber = 0.5;
1013
const percentage = `${decimalNumber * 100}%`;
1114

1215
return percentage;
13-
}
16+
``}
1417

1518
console.log(decimalNumber);
1619

17-
// =============> write your explanation here
20+
21+
// this is a function to convert a decimal number to a percentage - function declared
22+
// the decimal number is declared as 0.5
23+
// percentage is calculated by multiplying the decimal number by 100 and then adding the % symbol
24+
// the answer returned is named percentage and gives a number followed by %
25+
// the answer will be printed in console
1826

1927
// Finally, correct the code to fix the problem
20-
// =============> write your new code here
28+
// the error was actually 'decimalNumber' has already been declared
29+
// this was because decimalNumber was declared inside the function convertToPercentage(decimalNumber) and
30+
// declared again within the function with the const statement
31+
// once const decimalNumber = 0.5; was moved outside the function this error was resolved
32+
// that way the global decimalNumber exists and the function can still accept it as an argument
33+
34+
35+
// const decimalNumber = 0.5;
36+
// function convertToPercentage(decimalNumber) {
37+
// const percentage = `${decimalNumber * 100}%`;
38+
39+
// return percentage;
40+
}
41+
42+
// console.log(decimalNumber);
43+

0 commit comments

Comments
 (0)