Skip to content

Commit 598232d

Browse files
committed
convertToPercentage function
1 parent b573634 commit 598232d

File tree

1 file changed

+21
-6
lines changed
  • Sprint-2/1-key-errors

1 file changed

+21
-6
lines changed

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,34 @@
22

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5+
// I predict that the decimalNumber variable will be timed 100 and will get a percentage value of 50%, but that only my predictions.
56

67
// Try playing computer with the example to work out what is going on
78

8-
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
10-
const percentage = `${decimalNumber * 100}%`;
9+
// function convertToPercentage(decimalNumber) {
10+
// // const decimalNumber = 0.5;
11+
// const percentage = `${decimalNumber * 100}%`;
1112

12-
return percentage;
13-
}
13+
// return percentage;
14+
// }
1415

15-
console.log(decimalNumber);
16+
// console.log(decimalNumber);
1617

1718
// =============> write your explanation here
19+
// SyntaxError: Identifier 'decimalNumber' has already been declared
20+
// This error occurs because we have declared the variable decimalNumber twice,
21+
// once as a parameter and once as a variable inside the function.
22+
23+
// apparently there is another error that we only console logged the variable decimalNumber which is not declared in the global scope,
24+
// to fix this we instead call the function convertToPercentage with a decimal number as an argument and log the result of that function call to the console.
1825

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

0 commit comments

Comments
 (0)