FrontendAtlas
Study
▾
Dashboard
Pricing
Get full access
</> Code
Test cases
Language
JavaScript
TypeScript
export default function arrayForEach(arr, callbackFn, thisArg) { // Requirements: // - Snapshot initial length (do not visit appended elements beyond initial length) // - Skip holes (sparse slots) // - Skip elements deleted before their index is reached // - Bind thisArg using .call throw new Error('Not implemented'); }
import arrayForEach from './file'; describe('arrayForEach', () => { test('iterates in order', () => { const seen = []; arrayForEach([3, 1, 2], (v) => seen.push(v)); expect(seen).toEqual([3, 1, 2]); }); test('binds thisArg', () => { const ctx = { out: '' }; arrayForEach(['a', 'b'], function (v) { this.out += v; }, ctx); expect(ctx.out).toBe('ab'); }); test('skips holes (sparse arrays)', () => { // eslint-disable-next-line no-sparse-arrays const a = [1, , 3]; const seen = []; arrayForEach(a, (v, i) => seen.push([v, i])); expect(seen).toEqual([[1, 0], [3, 2]]); }); test('snapshots length (push during iteration not visited)', () => { const arr = [1, 2, 3]; const seen = []; arrayForEach(arr, (v, i, a) => { seen.push(v); if (i === 0) a.push(999); }); expect(seen).toEqual([1, 2, 3]); }); test('skips elements deleted before their turn (hole check)', () => { const arr = [1, 2, 3]; const seen = []; arrayForEach(arr, (v, i, a) => { if (i === 0) delete a[1]; seen.push(v); }); // index 1 was deleted before iteration reached it => skip it expect(seen).toEqual([1, 3]); }); });
▶ Run tests
Results
Console
Run tests to see results.