Skip to content

Instantly share code, notes, and snippets.

@bgoonz
Last active March 14, 2021 00:09
Show Gist options
  • Save bgoonz/4e2a040cd94006bb887a77a68f4287b9 to your computer and use it in GitHub Desktop.
Save bgoonz/4e2a040cd94006bb887a77a68f4287b9 to your computer and use it in GitHub Desktop.
let rotateRight = function (array, num) {
let result = array.slice(0);
for (let i = 0; i < num; i++) {
let ele = result.pop();
result.unshift(ele);
}
return result;
};
//let arr = ["a", "b", "c", "d", "e"];
console.log(rotateRight(arr, 2));
//["d", "e", "a", "b", "c"];
console.log(arr);
["a", "b", "c", "d", "e"];
let animals = ["wombat", "koala", "opossum", "kangaroo"];
console.log(rotateRight(animals, 3));
//["koala", "opossum", "kangaroo", "wombat"];
console.log(animals);
//["wombat", "koala", "opossum", "kangaroo"];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment