Skip to content

Commit d8487ec

Browse files
committed
1.js
1 parent ea7eebf commit d8487ec

File tree

1 file changed

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

1 file changed

+19
-6
lines changed

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

Lines changed: 19 additions & 6 deletions
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+
// An error will occur because 'decimalNumber' is redeclared inside the function using 'const',
6+
// which is not allowed. Also, 'decimalNumber' is not defined in the global scope,
7+
// so console.log(decimalNumber) will throw a ReferenceError.
58

69
// Try playing computer with the example to work out what is going on
710

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

12-
return percentage;
13-
}
15+
// return percentage;
16+
// }
1417

15-
console.log(decimalNumber);
18+
// console.log(decimalNumber);
1619

1720
// =============> write your explanation here
21+
// The error occurs because we cannot redeclare the parameter 'decimalNumber' inside the function
22+
// using 'const'. Also, 'decimalNumber' is not defined outside the function, so
23+
// console.log(decimalNumber) will throw a ReferenceError.
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.9)); // Output: 90%

0 commit comments

Comments
 (0)