Skip to content

Commit fcba7aa

Browse files
committed
Remove redeclaration of parameter and undefined variable usage in convertToPercentage
1 parent d957dd1 commit fcba7aa

File tree

1 file changed

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

1 file changed

+19
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
// Predict and explain first...
2+
// =============> write your prediction here
3+
// I predict this program will throw a `SyntaxError` because
4+
// the parameter `decimalNumber` is being redeclared inside the function using `const`.
5+
// Also, the variable `decimalNumber` is being accessed outside the function where it's not defined.
6+
27

38
// Why will an error occur when this program runs?
49
// =============> write your prediction here
@@ -15,6 +20,20 @@ function convertToPercentage(decimalNumber) {
1520
console.log(decimalNumber);
1621

1722
// =============> write your explanation here
23+
// The function already has a parameter named `decimalNumber`,
24+
// but inside the function, it's trying to declare another
25+
// constant with the same name: `const decimalNumber = 0.5;`.
26+
// JavaScript doesn’t allow redeclaring a variable that already exists in the same scope.
27+
// Also, `console.log(decimalNumber)` is outside the function and `decimalNumber` is not
28+
// defined in the global scope, so that will cause a `ReferenceError`.
29+
1830

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

0 commit comments

Comments
 (0)