Skip to content

Commit 3d852fa

Browse files
committed
question answered and code rewritten
1 parent e3b89c3 commit 3d852fa

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
@@ -2,9 +2,12 @@
22

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5+
// the code will throw an error because the variable str is being declared twice within the same scope
56

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

9+
/*
10+
811
function convertToPercentage(decimalNumber) {
912
const decimalNumber = 0.5;
1013
const percentage = `${decimalNumber * 100}%`;
@@ -14,7 +17,23 @@ function convertToPercentage(decimalNumber) {
1417
1518
console.log(decimalNumber);
1619
20+
*/
21+
1722
// =============> write your explanation here
23+
/* The error occurs because the variable 'decimalNumber' is declared twice within the same function scope using 'const'.
24+
This is not allowed in JavaScript, as each variable must have a unique name within its scope. When the function is called,
25+
the JavaScript engine encounters the second declaration of 'decimalNumber' and throws a SyntaxError indicating that the
26+
identifier has already been declared.*/
1827

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

0 commit comments

Comments
 (0)