Command Palette

Search for a command to run...

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

Set text on an element that may not exist

Write a function setText(id: string, text: string): void that puts text inside the element with that id.

If there is no element with that id, do nothing and do not crash. Do not use as or !.

The page already has a paragraph with the id status. The last two lines call your function with a real id and a missing one.

What Check tests

  1. Your code has no type errors
  2. The status paragraph now says Done
  3. The program reaches its last line (no crash on the missing id)
  4. No cast and no non-null assertion

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.