Task of Challenge #20

Rotate an array to the left 1 position. E.g. [1, 2, 3, 4, 5] -> [ 2, 3, 4, 5, 1 ] | [1, 2, 4, 5] -> [ 2, 4, 5, 1 ].

Solution

Explanation

You can achieve the challenge’s goal quite easily by using the shift() and the push() method for arrays. The shift() method removes the first element of an array and returns it. If we store it now into a variable (in this case first) then we can use the push() method to push it as the last element of an array.

If you want to dig deeper into shift() and push() you can read the Mozilla Dev Notes or take my JavaScript Course, where I introduce you also to all kinds of array methods (there are plenty of them and they are very useful).

To get a discount, just subscribe to my newsletter.

The output for the code above is:

Follow me on Instagram and don’t miss the latest Challenge!

https://www.instagram.com/arnold.code/

Become a bullet proof developer and seek my coding adventures on Udemy

https://www.udemy.com/user/arnold-abraham-3/

Don’t Miss My Most Read Article with over 8000 Views

One Reason Not to Migrate From JavaScript to TypeScript

Typescript saves a lot of development time, but is it worth an entire migrating process?

Get Your JavaScript Coding To A Very Professional Level with this article

Upgrade Your Legacy Coding Style With These 6 Modern Ways of Coding

And How These 3 Famous TV Shows Make You Write Excellent JavaScript Code