You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Feat: complete Sprint-2 mandatory and stretch challenges
Fixed key errors and debugged JavaScript functions
Implemented BMI calculator, case conversion, and unit conversion features
Added time formatting utilities with proper error handling
Completed all mandatory and stretch challenge requirements
Copy file name to clipboardExpand all lines: Sprint-2/1-key-errors/1.js
+20-1Lines changed: 20 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,8 @@
1
1
// Predict and explain first...
2
2
3
+
3
4
// Why will an error occur when this program runs?
4
-
// =============> write your prediction here
5
+
// I predict this code will cause a SyntaxError because of constant redeclaration, and even if that were fixed, it would cause a ReferenceError when trying to access decimalNumber outside the function
5
6
6
7
// Try playing computer with the example to work out what is going on
7
8
@@ -16,5 +17,23 @@ console.log(decimalNumber);
16
17
17
18
// =============> write your explanation here
18
19
20
+
// My explanation; There are two main problems:
21
+
// SyntaxError: The parameter decimalNumber and the constant const decimalNumber = 0.5; have the same name in the same scope, causing a redeclaration error.
22
+
// ReferenceError: Even if we fix the first issue, console.log(decimalNumber) tries to access a variable that only exists inside the function scope, not in the global scope.
23
+
24
+
25
+
19
26
// Finally, correct the code to fix the problem
20
27
// =============> write your new code here
28
+
29
+
// My code:
30
+
31
+
functionconvertToPercentage(decimalNumber){
32
+
constpercentage=`${decimalNumber*100}%`;
33
+
returnpercentage;
34
+
}
35
+
36
+
console.log(convertToPercentage(0.5));
37
+
38
+
// I removed the redundant redeclaration of decimalNumber inside the function
39
+
// I Changed console.log(decimalNumber) to actually call the function with convertToPercentage(0.5)
0 commit comments