Scriptdrill

Level 2: Arrays and objects Exercise 14 of 45 Pro

Zip two arrays

A JavaScript exercise from Scriptdrill. Solve it with a code editor in your browser that runs your function against example and hidden tests, and it is checked the moment you submit.

The task

Pair up the items of `a` and `b` by position: `[[a[0], b[0]], [a[1], b[1]], ...]`. Stop at the end of the shorter array. Items can be any value, including `null`, `0` and `false`.

What this exercise teaches

The shorter length decides how many pairs exist. Testing the values themselves (for example `a[i] && b[i]`) would stop early at the first falsy item.

A hint if you get stuck

Loop up to `Math.min(a.length, b.length)`. Looking at the lengths, not at the values, is what makes `null` and `0` safe.

This exercise is part of Pro

Pro unlocks all 45 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.

You create a free account first, so Pro is attached to it.

Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.

Start with the free exercises

More exercises in Arrays and objects