title: What's wrong with my array rotation algorithm?
body: <p>I was working on Codility on a challenge to rotate an array k times. I came up with this solution which in theory should work but in reality only got a 25% score.</p>
  
  <p>Problem Statement</p>
  
  <blockquote>
    <p>zero-indexed array A consisting of N integers is given. Rotation of
    the array means that each element is shifted right by one index, and
    the last element of the array is also moved to the first place.</p>
    
    <p>For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9,
    7]. The goal is to rotate array A K times; that is, each element of A
    will be shifted to the right by K indexes.</p>
    
    <p>Write a function:</p>
    
    <p>function solution(A, K);</p>
    
    <p>that, given a zero-indexed array A consisting of N integers and an
    integer K, returns the array A rotated K times.</p>
    
    <p>For example, given array A = [3, 8, 9, 7, 6] and K = 3, the function
    should return [9, 7, 6, 3, 8].</p>
    
    <p>Assume that:</p>
    
    <p>•N and K are integers within the range [0..100]; •each element of
    array A is an integer within the range [−1,000..1,000].</p>
  </blockquote>
  
  <pre><code>function solution(A, K) {
      for (var i = 1; i &lt; K; i++) {
          A.push(A.shift());
      }
      return A;
  }
  </code></pre>
  
  <p>Variable A represents an array, and variable K represents the number of rotations needed.</p>
  
  <p>To add an element to the end of an array, the push function is used. Whereas the shift function removes the first element of an array. This seems like what a rotation would be.</p>
  
  <p>** Test Results**</p>
  
  <p>empty-array: passed</p>
  
  <p>single: One element (0 &lt;= k &lt;= 5): Passed</p>
  
  <p>double: Two elements (k &lt;= n): Failed</p>
  
  <p>small1: Failed</p>
  
  <p>small2: Failed</p>
  
  <p>maximal: Maximal n and k: Failed</p>