'Codility CyclicRotation Javascript Solution: Empty array returns an error

I am trying to solve this problem in codility. My solution passed all the tests except for one with empty array and rotation of 1 as arguments. I'm someway lost of how to even approach for solving this issue. Could someone please nudge me in the right direction? Other than writing a specific if clause for an empty array check. I cannot come up with a more elegant solution.

function solution(A, K)
{
    for (i=0; i<K; i++)
    {
        A.unshift(A.pop());
    }
    return A;
}


Solution 1:[1]

One solution is to check for A.length on the loop conditions section.

function solution(A, K)
{
    for (i = 0; i < K && A.length; i++)
    {
        A.unshift(A.pop());
    }
    return A;
}

console.log(solution([3, 8, 9, 7, 6], 3));
console.log(solution([], 5));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

However, there is an alternative approach you can do using Array.slice():

function solution(A, K)
{
    let i = A.length - (K % (A.length || 1));
    return [...A.slice(i), ...A.slice(0, i)];
}

console.log(solution([3, 8, 9, 7, 6], 3));
console.log(solution([], 0));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

Solution 2:[2]

Try this:

let solution = (A, K) => {
    for (i=0; i<K; i++){
        A.unshift(A.pop());
    }
    return A[0] != undefined ? A : [];
}

Solution 3:[3]

An alternative way to solve this could be via recursion. In this case, you can use an if statement, however, this serves as the base case for the termination of your recursive calls. The base case here is if K === 0 meaning if we don't need to rotate the array anymore, then return the array. It also has another part where we return the array if !A.length. This means that if the array is empty also return the current array.

function solution(A, K) {
  if(K === 0 || !A.length) return A;
  return solution([A.pop(), ...A], K-1);  
}

console.log(solution([1, 2, 3, 4], 2)); // [3, 4, 1, 2]
console.log(solution([], 10)); // []

The above can be rewritten as a one-liner if you wish:

const f = (A, K) => K === 0 || !A.length ? A : f([A.pop(), ...A], K-1);

You can visualize the function above performing the following (let the function solution be f for our example):

f([1, 2, 3, 4], 2) = f([4] + [1, 2, 3], 1) = f([4, 1, 2, 3], 1)

f([4, 1, 2, 3], 1) = f([3, 4, 1, 2], 0) --> [3, 4, 1, 2] // we return the array instead of another function call as K === 0 (our base case)

Or when the array is empty:

f([], 10) = [] // instantly return our array as it is empty. The clause !A.length is true (also part of our base case) which means we return A (our array) instantly.

Solution 4:[4]

Another alternative that got 100% across the board:

function solution(A, K) {
    const k = K % A.length; // in case the rotation is bigger than the length of the array
    if (K === 0 || k === 0) {
      return A;
    }
    const head = A.slice(0, A.length - k);
    const tail = A.slice(A.length - k, A.length);
    return [...tail, ...head];
}

Solution 5:[5]

Just got 100%.

function cyclicRotation(A, K){
    K = K % A.length
    let sliceA = A.slice(A.length-K, A.length)
    let sliceB = A.slice(0, A.length-K)
    return sliceA.concat(sliceB)
}

Solution 6:[6]

My 100% JavaScript solution that avoids changing the value of A parameter by creating a copy of A beforehand:

function solution(A, K) {

    const rotatedA = [...A];

    for (let i = 0; i < K; i++) {
        rotatedA.length > 0 && rotatedA.unshift(rotatedA.pop());
    }

    return rotatedA;
    
}

Solution 7:[7]

Got 100%, please give me your opinion

function solution(A, K) {
    let returnedArray = [...A];
    if((A.length === K || A.length === 0 || isAllValueTheSame(A))){
        return A
    }
    for(let i = 0;i<K;i++){
        returnedArray = rotate(returnedArray)
    }
    return returnedArray
}

function isAllValueTheSame (A){
    const firstValue = A[0]
    return A.every((value) => value === firstValue)
}

function rotate (A){
    let tempArray = [...A];
    tempArray[0] = A[A.length-1] // the last goes first
    for(let i = 0 ; i < A.length -1 ;i++){
        tempArray[i+1] = A[i]
    }
    return tempArray
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Artee
Solution 3
Solution 4 Nick
Solution 5 LLe
Solution 6 noviewpoint
Solution 7 Roee Angel