title: Rook on a Phone keypad - Javascript
body: <p>I'm trying to write/come up with an algorithm for a phone keypad traversal. </p>
  
  <p>Lets say I have a rook on a keypad. The rook can traverse only horizontally and vertically. My code has to input a phone number and check whether the rook is able to dial it(shouldn't go diagonal ways) and return a boolean based on the result.</p>
  
  <p><a href="https://i.stack.imgur.com/576Tf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/576Tf.png" alt="enter image description here"></a></p>
  
  <p>Example: A phone number "4632871" is termed as "true" since the rook can traverse without going diagonally whereas "4853267" is termed as "false"</p>
  
  <p>Below is my code implementation:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var data = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ]
  
  function rookTraversal(arr, phoneNumber) {
  
    var arrIndex = [],
      numArray = [];
  
    var number = phoneNumber.split('')
  
    for (var i = 0; i &lt; arr.length; i++) {
      for (var j = 0; j &lt; arr.length; j++) {
        arrIndex.push([i, j])
      }
    }
    arrIndex = arrIndex.map((item, index) =&gt; {
      return {
        [index + 1]: item
      }
    })
    number.forEach((item, index) =&gt; {
      numArray.push(arrIndex[item - 1])
    })
  
    let numArrayKeys = numArray.reduce((acc, x) =&gt; [...acc, Object.values(x).map((y, i) =&gt; y)], []);
  
  
    console.log(numArrayKeys)
  
  }
  
  
  
  console.log(rookTraversal(data, "4632871"))</code></pre>
  </div>
  </div>
  </p>
  
  <p>I'm not sure how to proceed after this. Can someone help me out please?</p>
  
  <p>Thanks.</p>