Task of Challenge #22

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

The function reverseArray just creates an empty array ar2 where the reversed one gets into. A simple for loop covers the tasks of reversing our array. It does it by decrementing a running index of i from the array.length towards 0 and pushing therefore from the last element of the passed array until the first into ar2.

After the for loop, the function just returns your newly reversed array of ar2.

If you want to dig deeper into arrays and their methods 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:

Become a Bullet Proof Developer and Seek My Coding Adventures

https://arnoldcodeacademy.teachable.com/p/home

Sharpen Your JavaScript Coding Skills by Creating GOOD Code

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