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
- Your code has no type errors
- The status paragraph now says Done
- The program reaches its last line (no crash on the missing id)
- 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.