Well, creating games like this can be really hard. You need to use some structure to make this work. You basically need to build up a multidimensional array and keep track of what place is used for a word.
This should push you in the right direction: https://mitchum.blog/building-a-crossword-puzzle-generator-with-javascript/