Skip to content

Created a function rotation to made the rotation of an array #53 and #52 #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Array/Rotation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;