Skip to content
dreamcode
dreamcode
Map
TS Basics Challenge
Section challenge · BeginnerTypeScript
Reward: +40 XP
PROBLEM

TS Basics Challenge

Write a function checkStar(star) that accepts an object conforming to the interface Star { name: string; magnitude?: number }. The function should:

  1. If name is empty (length 0), return 'Invalid Star'.
  2. If magnitude is present, return 'Magnitude: ' followed by the magnitude value.
  3. If magnitude is missing, return 'Magnitude: unknown'.
Examples
checkStar({ name: "Vega", magnitude: 0.03 })
"Magnitude: 0.03"
checkStar({ name: "Polaris" })
"Magnitude: unknown"
solution.ts
TYPESCRIPT
Saved as you type

Tests

0 of 3 passing
  • Vega → Magnitude: 0.03
    checkStar({ name: "Vega", magnitude: 0.03 })
    expected "Magnitude: 0.03"
  • Polaris no magnitude → unknown
    checkStar({ name: "Polaris" })
    expected "Magnitude: unknown"
  • Empty name → Invalid Star
    checkStar({ name: "", magnitude: 1.5 })
    expected "Invalid Star"
On the line
+40 XP
Pass all 3 tests to claim it.