Shift Array by First and Last Item

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);

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store