const nums = [1,2,3,4,5,6,7]
const k = 2
const Output = [5,6,7,1,2,3,4]
//Explanation:
/* rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4] */
function shiftArr (arr, n=1){
let r1 = []
let r2 = []
for(let i = 0 ;i < arr.length; i++){
if(i>=arr.length - n ){
r2.push(arr[i])
}else{
r1.push(arr[i])
}
}
return [...r2, ...r1]
}
console.log(shiftArr(nums, k))
To embed this project on your website, copy the following code and paste it into your website's HTML: