Files
freeCodeCamp/tools/challenge-helper-scripts/helpers/utils.ts
2025-09-02 16:03:28 +02:00

9 lines
246 B
TypeScript

export function insertInto<T>(arr: T[], index: number, elem: T): T[] {
if (index >= arr.length) return [...arr, elem];
if (index <= 0) return [elem, ...arr];
return arr.flatMap((x, id) => {
return id === index ? [elem, x] : x;
});
}