

TS Unions & Enums Challenge
Write a function parseSpeed(speed) that takes a union type parameter speed: string | number. The function should:
- If
speedis a number, return it directly. - If
speedis a string, parse it usingparseFloat(). If the parsed value is not a number (isNaN) or is negative, return-1. - Otherwise, return the parsed number.
Examples
parseSpeed(120)
→ 120
parseSpeed("85.5mph")
→ 85.5
solution.ts
TYPESCRIPT
Saved as you type
Tests
0 of 4 passing- •number 120 → 120parseSpeed(120)expected 120
- •string '85.5mph' → 85.5parseSpeed("85.5mph")expected 85.5
- •invalid string 'fast' → -1parseSpeed("fast")expected -1
- •negative number -50 → -1parseSpeed(-50)expected -1
On the line
+50 XP
Pass all 4 tests to claim it.
