Skip to content

Commit 83a2f87

Browse files
committed
Solved task 1-key-errors/1.js
1 parent e113639 commit 83a2f87

File tree

1 file changed

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

1 file changed

+18
-6
lines changed

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

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

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5+
// I suppose the error could be:
6+
// 1. The variable "decimalNumber" declared twice in the same scope (in the function's parameter and in the function's body)
7+
// 2. We can't reassign a value if we use word "const" to declare a variaable
8+
// 3. The variable "decimalNumber" isn't defined in the global scope, so when we try to use function console.log(decimalNumber) it will throw a ReferenceError. If we want to log the result of the function we have to call this function with some argument: console.log(convertToPercentage(0.5))
59

610
// Try playing computer with the example to work out what is going on
711

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

12-
return percentage;
13-
}
16+
// return percentage;
17+
// }
1418

15-
console.log(decimalNumber);
19+
// console.log(decimalNumber);
1620

1721
// =============> write your explanation here
22+
//So, when I run the code, I got "SyntaxError: Identifier 'decimalNumber' has already been declared" and that the same as my prediction above. I didn't think that actually we don't need to declare "decimalNumber" again inside the function, and I removed line "const decimalNumber = 0.5; to fix the problem. THen, I called the function with the argument 0.5 in console.log and this worked fine.
1823

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

0 commit comments

Comments
 (0)