The NaN Guardian
HardAcc. 51.3%
+55 XP 25
Handling Math Failures
In JavaScript, subtracting a string like "Apple" from a number results in NaN. This can break your entire app if not handled. We need a "Guardian" that ensures a number is always returned.
The Assignment
Your function receives parameters a and b (they could be any type).
- Subtract
bfroma(a - b). - If the result is a valid number, return it.
- If the result is
NaN, return the number0.
01EXAMPLE 1
Input
a = 10, b = "Apple"Output
0Explanation: 10 - "Apple" is NaN, so we return 0.
Constraints
- Detect NaN using Number.isNaN().
- Return a number, never NaN.
MathLogicMastery
JavaScriptSystem handles I/O — write your function only
Loading...
3 Hidden
Input Arguments
a10
b"Hi"
Expected Output
0
Click RUN to test your solution