Command Palette

Search for a command to run...

Hectal
Phase 13: TypeScript in the Browser: DOM and Async
TypeScript · Exercise 2 of 6 PreviousNext
Beginner

Read a number from an input, or null

Write readNumber(id: string): number | null. It returns the number typed into the <input> with that id.

It returns null when the element does not exist, is not an <input>, is empty, or does not contain a number. Use instanceof, not a cast.

The page has an input with 42, an input with abc, and a <div> that contains 12. The program logs the result for each, and for a missing id.

What Check tests

  1. Your code has no type errors
  2. The four calls print 42, null, null, null
  3. The result can be stored as number | null
  4. The result cannot be treated as a plain number
  5. No cast is used

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Your page

Press Run to see the page.

Console

Nothing logged yet.