Shift Array by First and Last Item

Cing Sian Dal
1 min readJan 27, 2020

There is an array.

If the most right first item is replaced by another different value, the array has to shift to the right without changing the fixed array count. If the most left last item is replaced by another different value, the array has to shift to the left without changing the fixed array count.

x = [1,2,3,4,5];function leftSwipe(newItem){
x.pop();
x = [newItem, ...x];
}
function rightSwipe(newItem){
x.shift();
x = [...x, newItem]
}
leftSwipe(3);
console.log(x);
rightSwipe(10);
console.log(x);

--

--

Cing Sian Dal

Don’t follow me. I wrote junks here. Follow me on Twitter instead.