Task of Challenge #21

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

Solution

Explanation

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

If you want to dig deeper into pop() and unshift() 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