I am creating a wordsearch generator with JavaScript I created the function that places the word, it is working as expected but I created a list of words to add them to the wordsearch in a forloop the problem it's that it just adds one word
this is the code I wrote
function placeWord(p,grid) {
for (i=0;i<p.length;i++) {
grid[p[i][0]][p[i][1]] = p[i][2]
}
}
function tryPlaceWord(word, grid, width, height) {
let s = 0
var directions = [[0,1], [1,0], [1,1]]
var choices = [word, word.split("").reverse().join("")]
var word = choices[Math.floor(Math.random() * 2)]
while (s<2) {
var direction = directions[ Math.floor(Math.random() * 3) ]
var x = Math.floor(Math.random() * width)
var y = Math.floor(Math.random() * height)
var ystart = y+word.length > height ? y-word.length:y
var xstart = x+word.length > width ? x-word.length:x
var positions = []
for (i=0;i<word.length;i++) {
if (xstart < 0) {
break
}
if (ystart<0) {
break
}
var xpos = xstart + i * direction[1]
var ypos = ystart +i * direction[0]
pos = grid[ypos][xpos]
if (pos == "*" || pos == word[i]) {
positions[i] = [ypos, xpos, word[i]]
} else {
positions.length = 0
break
}
}
if (positions.length == word.length) {
placeWord(positions,grid)
console.log(positions)
return true
}
s+=1
}
}
function show(grid) {
for (i=0;i<grid.length;i++) {
console.log(grid[i].join(" "));
}
}
function makeGrid(width, height) {
var grid = []
for (i=0;i<height;i++) {
grid[i] = []
for (y=0;y<width;y++) {
grid[i][y] = "*"
}
}
return grid
}
function main() {
var width = 15
var height = 15
var grid = makeGrid(width, height)
var words = ["alexander", "camila", "pedro", "luis", "maria"]
for (i=0;i<words.length;i++) {
console.log(words[i])
tryPlaceWord(words[i],grid,width, height)
}
show(grid)
}
main()
any help would be really appreciated