Skip to content

Commit caa6042

Browse files
change variable scope
1 parent 9e6672f commit caa6042

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

  • Sprint-3/1-key-errors

‎Sprint-3/1-key-errors/1.js‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,27 @@
22

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5-
5+
// If we run the program, I predict we will get an error.
6+
// The first problem is that 'decimalNumber' is already function parameter, but you try to declare another variable with the same name inside the function.
7+
//another problem is that 'decimalNumber' is a local variable inside the function
8+
// and we cannot access to it outside the function.
9+
//------------------------------------------------------
610
// Try playing computer with the example to work out what is going on
11+
//SyntaxError: Identifier 'decimalNumber' has already been declared
12+
13+
// =============> write your explanation here
14+
15+
// The error occurs because 'decimalNumber' has already been declared
16+
// as a function parameter. we cannot declare it again with 'const'
17+
18+
// Finally, correct the code to fix the problem
19+
// =============> write your new code here
20+
const decimalNumber = 0.5;
721

822
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
1023
const percentage = `${decimalNumber * 100}%`;
1124

1225
return percentage;
1326
}
1427

15-
console.log(decimalNumber);
16-
17-
// =============> write your explanation here
18-
19-
// Finally, correct the code to fix the problem
20-
// =============> write your new code here
28+
console.log(convertToPercentage(decimalNumber));

0 commit comments

Comments
 (0)