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).

  1. Subtract b from a (a - b).
  2. If the result is a valid number, return it.
  3. If the result is NaN, return the number 0.

01EXAMPLE 1

Inputa = 10, b = "Apple"
Output0

Explanation: 10 - "Apple" is NaN, so we return 0.

Constraints

  • Detect NaN using Number.isNaN().
  • Return a number, never NaN.
MathLogicMastery
JavaScript
Loading...
3 Hidden

Input Arguments

a10
b"Hi"

Expected Output

0

Click RUN to test your solution