DEV Community

Cover image for Let's Make Arrays Dance: 4 Approaches to Rotate an Array in JavaScript
Valentina
Valentina

Posted on

Let's Make Arrays Dance: 4 Approaches to Rotate an Array in JavaScript

Let's Make Arrays Dance: 4 Approaches to Rotate an Array in JavaScript

One frequently encountered task in programming interviews and problem-solving is the rotation of an array. The versatility of JavaScript as a language presents us with many intriguing ways to go about this. Let's navigate through these various approaches.

1. The Straightforward Shuffle Using Splice and Concat

As you venture into the realm of JavaScript, one simple and straightforward way to address this task awaits you: using the splice() and concat() methods. It's undeniably effective!

Here's how it looks:

function rotateArray(array, steps) {
    return array.concat(array.splice(0, steps));
}
Enter fullscreen mode Exit fullscreen mode

Let's dissect how this works:

  • The splice() method extracts the initial elements of our array, the number of which is determined by our variable steps. These elements are removed from the original array.
  • The concat() method appends the extracted elements to the end of the remaining elements in the array.

And just like that, we have our rotated array! πŸ˜‰

  • Word of caution: This method is suitable when you are explicitly permitted to use built-in JavaScript functions. If this isn't the case, stick around for the other techniques we're about to discuss.

2. The Classic Spin: Looping through the Array

To flex your core JavaScript knowledge during an interview, you might want to lean on the traditional for loop.

How about spicing it up a little with ES6 syntax? Sounds good, right?

function rotateArray(array, steps) {
    let result = [...array];
    for(let i = 0; i < steps; i++){
        result.push(result.shift());
    }
    return result;
}
Enter fullscreen mode Exit fullscreen mode

The modern for loop syntax introduced by ES6 is both user-friendly and less error-prone. Plus, it just looks so sleek! πŸ‘Œ

3. The Trendy Twist: Array Destructuring

ES6 brought another nifty feature into JavaScript: array destructuring, and it fits perfectly into our current narrative.

This approach is akin to the first method we discussed, but with a modern twist. Here's how it goes:

function rotateArray(array, steps) {
    let newArray = [...array];
    let shiftArray = newArray.splice(0, steps);
    return [...newArray, ...shiftArray];
}
Enter fullscreen mode Exit fullscreen mode

Cool, right? 😎

The splice() method extracts the elements from our original array. The spread operator ... is then used to destructure these elements and concatenate them with the remaining elements in the array.

4. And Finally... The Recursive Rotation!

The last method I'm excited to share is a recursive approach to our array rotation problem.

This technique uses the function to call itself recursively until we reach our base case, which in this instance is when the steps variable reaches zero.

We can even use the ternary operator to make our JavaScript syntax more concise and clean.

function rotateArray(array, steps) {
    return steps === 0 ? array : rotateArray(array.slice(1).concat(array[0]), steps-1);
}
Enter fullscreen mode Exit fullscreen mode

And that concludes our dance with array rotation using JavaScript! πŸŽ‰

I sincerely hope you found this tutorial insightful. Do share your thoughts and your own unique solutions too!

Top comments (0)