forked from akshitagit/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotation.js
More file actions
26 lines (24 loc) · 704 Bytes
/
Rotation.js
File metadata and controls
26 lines (24 loc) · 704 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* A function that takes 3 parameters to make a rotation in an array.
* @example arr=[1,2,3,4,5,6,7,8,9], rotation(arr, 3, "right") returns [4,5,6,7,8,9,1,2,3]
* @function
* @param {Array} arr - The array to rotate.
* @param {Number} n - The number of rotations.
* @param {string} direction - The direction of the rotation.
* @returns {Array}
*/
const rotation = (arr, n, direction) => {
if (direction === "right") {
for (let i = 0; i < n; i++) {
let firstElement = arr.shift();
arr.push(firstElement);
}
} else {
for (let i = 0; i < n; i++) {
let lastElement = arr.pop();
arr.unshift(lastElement);
}
}
return arr;
};
export default rotation;