Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function rotateArray(array, direction, times) {
- let arrayResult = array.slice(0);
- if (direction === "left") {
- for (let i = 0; i < times; i++) {
- let taken = arrayResult.shift();
- arrayResult.push(taken);
- }
- }
- else if (direction === "right") {
- for (let i = 0; i < times; i++) {
- let taken = arrayResult.pop();
- arrayResult.unshift(taken);
- }
- }
- else {
- console.log("invalid direction");
- }
- return arrayResult;
- }
Advertisement
Add Comment
Please, Sign In to add comment