Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- function rotateArray(arr) {
- let rotations = Number( arr.pop() );
- if ( isNaN(rotations) ) {
- console.log('Empty');
- return;
- }
- //Rotations
- for (let i = 0; i <= rotations - 1; i++) {
- let lastElement = arr[arr.length - 1];
- //Move all elements to the right
- for (let j = arr.length - 1; j >= 0; j--) {
- arr[j] = arr[j - 1];
- }
- arr[0] = lastElement;
- }
- console.log( arr.join(' ') );
- }
- rotateArray(['remove', 'remove', 'remove']);
- [1, 2, 3, 4]
- [4, 1, 2, 3]
- [3, 4, 1, 2]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement