title: Check if a string exists in a 2D matrix in javascript?
body: <p>I am trying to find horizontalWord string in two-dimensional array. verticalWord works fine , but I am having trouble with the horizontalWord string. Let me know if you have any ideas.</p>
  
  <pre><code> 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 &lt; matrix[0].length; j++) {
  
      let verticalWord = ''
      let horizontalWord = ''
  
      for (let i = 0; i &lt; matrix.length; i++) {
        verticalWord += matrix[i][j]
      }
  
      for (let k = 0; k &lt; 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'))
  </code></pre>