Skip to content

Commit 73c7f27

Browse files
committed
Predict, explain and solve 1.js in errors
1 parent 0da90b9 commit 73c7f27

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

  • Sprint-3/1-key-errors

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5+
// Answer: I will get a Syntax Error, because the variable decimalNumber is a parameter
6+
// of the function, and then it gets re-declared inside the function. Removing the
7+
// "const" would make sure it's not re-declared, just value reassigned on that line.
58

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

11+
/*
812
function convertToPercentage(decimalNumber) {
913
const decimalNumber = 0.5;
1014
const percentage = `${decimalNumber * 100}%`;
@@ -13,8 +17,26 @@ function convertToPercentage(decimalNumber) {
1317
}
1418
1519
console.log(decimalNumber);
20+
*/
1621

1722
// =============> write your explanation here
23+
// Answer: I got SyntaxError: Identifier 'decimalNumber' has already been declared
24+
// I will make sure it's not re-declared in the function by removing the "const"
25+
26+
// After removing the "const" I got a new error. ReferenceError: decimalNumber is not defined. I realized
27+
// that the console.log at the bottom didn't call the function but just logged decimalNumber which hadn't
28+
// been defined in the global scope. Putting back the "const", I will move the declaration of decimalNumber
29+
// to outside the function to make the scope global. Now the global decimalNumber has nothing to do with the
30+
// local parameter decimalNumber in the function.
1831

1932
// Finally, correct the code to fix the problem
2033
// =============> write your new code here
34+
function convertToPercentage(decimalNumber) {
35+
const percentage = `${decimalNumber * 100}%`;
36+
37+
return percentage;
38+
}
39+
40+
const decimalNumber = 0.5;
41+
42+
console.log(decimalNumber);

0 commit comments

Comments
 (0)