Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function arrayRotation(arr, rotations) {
- //Number of rotations loop
- for (let i = 0; i < rotations; i++) {
- let firstElement = arr[0];
- //Move all elements to the left
- for (let j = 0; j < arr.length - 1; j++) {
- arr[j] = arr[j + 1];
- }
- //Set last element to the old first element
- arr[arr.length - 1] = firstElement;
- }
- //Stringify array
- let arrayString = '';
- for (let i = 0; i < arr.length; i++) {
- if (i == arr.length - 1) {
- arrayString += arr[i];
- } else {
- arrayString += arr[i] + ' ';
- }
- }
- //Output
- console.log(arrayString);
- }
- arrayRotation(
- [51, 47, 32, 61, 21], 2
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement