Skip to content

Commit 2302130

Browse files
committed
update: convertToPercentage function to fix redeclaration error and explain Sprint-2/1-key-errors/1.js
1 parent 6e9aa77 commit 2302130

File tree

1 file changed

+26
-7
lines changed
  • Sprint-2/1-key-errors

1 file changed

+26
-7
lines changed

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

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,35 @@
55

66
// Try playing computer with the example to work out what is going on
77

8-
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
10-
const percentage = `${decimalNumber * 100}%`;
11-
12-
return percentage;
13-
}
8+
// function convertToPercentage(decimalNumber) {
9+
// const decimalNumber = 0.5;
10+
// The above line will cause an error because 'decimalNumber' is being redeclared with 'const'
11+
// after it has already been defined as a parameter of the function.
12+
// This will result in a SyntaxError: Identifier 'decimalNumber' has already been declared.
13+
// it is trying to declare a new constant variable with the same name as the parameter of the function: decimalNumber
14+
// const percentage = `${decimalNumber * 100}%`;
15+
// The above line correctly converts the decimal number to a percentage by multiplying it by 100
16+
// and appending a '%' sign.
17+
// However, the function will not execute due to the redeclaration error.
1418

15-
console.log(decimalNumber);
19+
// return percentage;
20+
// // }
1621

22+
// console.log(decimalNumber);
23+
// console.log(decimalNumber) outside the function — but decimalNumber was only defined inside the function.
1724
// =============> write your explanation here
1825

1926
// Finally, correct the code to fix the problem
2027
// =============> write your new code here
28+
function convertToPercentage(decimalNumber) {
29+
const percentage = `${decimalNumber * 100}%`;
30+
return percentage;
31+
}
32+
// =============> write your explanation here
33+
// The function now correctly takes a decimal number as input, converts it to a percentage by multiplying it by 100,
34+
// and returns the result as a string with a '%' sign appended. The redeclaration error has been fixed by removing the
35+
// unnecessary declaration of 'decimalNumber' inside the function. The function can now be called with a decimal number,
36+
// and it will return the corresponding percentage string.
37+
console.log(convertToPercentage(0.5)); // "50%"
38+
39+
// The function now works correctly and returns the expected output without any errors.

0 commit comments

Comments
 (0)