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
- Your code has no type errors
- The four calls print 42, null, null, null
- The result can be stored as number | null
- The result cannot be treated as a plain number
- 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.