Skip to content

Commit 6128237

Browse files
committed
Complete error exercise 1 - convertToPercentage
1 parent 47a9c16 commit 6128237

1 file changed

Lines changed: 7 additions & 10 deletions

File tree

  • Sprint-3/1-key-errors

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
// Predict and explain first...
2-
32
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
3+
// Prediction: SyntaxError — decimalNumber is a parameter and is redeclared with const inside the function.
4+
// Also, console.log(decimalNumber) outside the function would cause ReferenceError (not in that scope).
55

6-
// Try playing computer with the example to work out what is going on
6+
// Explanation:
7+
// 1) You cannot declare const decimalNumber when decimalNumber is already a parameter in the same scope.
8+
// 2) decimalNumber only exists inside the function, so logging it outside fails.
9+
// Fix: use the parameter (do not reassign it) and call the function in console.log.
710

811
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
1012
const percentage = `${decimalNumber * 100}%`;
11-
1213
return percentage;
1314
}
1415

15-
console.log(decimalNumber);
16-
17-
// =============> write your explanation here
16+
console.log(convertToPercentage(0.5));
1817

19-
// Finally, correct the code to fix the problem
20-
// =============> write your new code here

0 commit comments

Comments
 (0)