Harry’s magic wand breaks at a random point (location of the point is uniform along the length of the stick, which is $40$ cm long). Suppose the piece of the stick that Harry is left with is $X$ cm long. Unfortunately, the next day part of the stick is accidentally burnt while casting a spell. After this accident, the length of the stick is reduced to $D$ cm, where $D$ is uniformly distributed between $[0,X]$. Find $f_D(d)$.
The solution is shown here...
What I don't understand is why the bounds of integration in the final step go from $d$ to $40$, the combined regions of the wand destroyed between the two accidents. Why shouldn't the integration go, for example, from $0$ to $d$ (the portion of the wand remaining), or from $0$ to $40$ (the entire initial wand)?

This because the joint domain $(X,D)$ is a triangle:
$$0<\underbrace{d<x<40}_{\text{X-support}}$$
In fact D is the length after the second accident and thus it cannot be longer than X.
to find $f_D(d)$ you have to integrate the joint density over all X domain, say
$x \in (d;40)$