JSFiddle - React, Tailwind, and code Playground
by jonahe
JavaScript
const shiftPosition = (array, indexToShift, shiftAmount) => {
const itemToShift = array[indexToShift];
if(!itemToShift) console.error(`could not find item at index ${indexToShift}`);
const newIndex = indexToShift + shiftAmount;
if(newIndex < 0 || newIndex >= array.length) console.error(`can't shift, would result in invalid index of ${newIndex}`);
const itemsAhead = array.slice(0, indexToShift);
console.log("itemsAhead", itemsAhead);
}
const samples = ["a","b", "c", "d"];
shiftPosition(samples, 2, 4);