Skip to content

Commit 2068031

Browse files
committed
completion of 1-key-errors
1 parent c786fbc commit 2068031

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

  • Sprint-2/1-key-errors

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,28 @@
22

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5+
// The error will occur because we are trying to declare a variable with the same name as the parameter of the function. The error message will likely indicate that there is a syntax error or that the variable 'str' has already been declared.
56

67
// Try playing computer with the example to work out what is going on
78

8-
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
10-
const percentage = `${decimalNumber * 100}%`;
9+
//function convertToPercentage(decimalNumber) {
10+
//const decimalNumber = 0.5;
11+
//const percentage = `${decimalNumber * 100}%`;
1112

12-
return percentage;
13-
}
13+
//return percentage;
14+
// }
1415

15-
console.log(decimalNumber);
16+
//console.log(decimalNumber);
1617

1718
// =============> write your explanation here
19+
// The error is occurring because we are trying to declare a variable 'decimalNumber' inside the function that has the same name as the parameter 'decimalNumber'. To fix this problem, we can different variable name for the parameter or the variable inside the function. For example, we could change the line to: const decimalNum = 0.5; and then use 'decimalNum' instead of 'decimalNumber' in the rest of the function.
1820

1921
// Finally, correct the code to fix the problem
2022
// =============> write your new code here
23+
24+
function convertToPercentage(decimalNumber) {
25+
const decimalNum = 0.5;
26+
const percentage = `${decimalNum * 100}%`;
27+
return percentage;
28+
}
29+
console.log(convertToPercentage(0.5));

0 commit comments

Comments
 (0)