Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function rotate(arr) {
- const arrLength = arr.length;
- const lastElement = arr[arrLength - 1];
- for (let index = arrLength - 1; index > 0; index--) {
- arr[index] = arr[index - 1];
- }
- arr[0] = lastElement;
- }
- function rotateArray(arr, rotations) {
- for (let i = 0; i < rotations; i++) {
- rotate(arr);
- }
- console.log(arr.join(" "));
- }
- rotateArray(['1', '2', '3', '4'], 2);
- rotateArray(['Banana', 'Orange', 'Coconut', 'Apple'], 15);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement