First check if Neocities allows making custom 404 page.
If yes, find it location and add html/js content to it.
As for "very detailed explanation" of javascript please read some documentation. It is impossible teach it here.
You can find articles on StackOverflow. For example Random image generation in Javascript