I am currently working on a script to locate the horizontalWord string within a two-dimensional array. While my function for finding verticalWord strings is running smoothly, I am encountering some difficulties with the horizontalWord string. If you have any suggestions or solutions, please do not hesitate to share them.
let matrix = [
[0, 'r', 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
['a', 'p', 'p', 'l', 'e'],
[0, 0, 0, 0, 0]
]
function isInMatrix(matrix, word) {
for (let j = 0; j < matrix[0].length; j++) {
let verticalWord = ''
let horizontalWord = ''
for (let i = 0; i < matrix.length; i++) {
verticalWord += matrix[i][j]
}
for (let k = 0; k < matrix[0].length; k++) {
horizontalWord += matrix[j][k]
}
if ((verticalWord.includes(word)) ||
(verticalWord.split('').reverse().join('').includes(word)) ||
(horizontalWord.includes(word)) ||
(horizontalWord.split('').reverse().join('').includes(word))) return true
}
return false
}
console.log(isInMatrix(matrix, 'apple'))