AniUI Academy

Unzip rows back into columns

mediumArrays & Objects+250 XPPractice

A table comes back from the server as rows. Your chart wants columns: all the x values in one array, all the y values in another. That is a transpose.

Implement unzip(rows) where rows is an array of arrays. Return an array of columns: column j contains the element at index j from each row, in row order. The result has as many columns as the longest row. Where a shorter row has no element at that index, use null. An empty rows array returns an empty array. Applying unzip twice to a rectangular input gives back the original structure.

Arguments

rowsArray<Array>
A list of rows to transpose into columns.

Returns

Array<Array> — One array per column of the widest row, each in row order. Missing entries become null.

Examples

unzip([[1, "a"], [2, "b"]]);
// [[1, 2], ["a", "b"]]
unzip([[1, 2], [3]]);          // [[1, 3], [2, null]] — ragged rows pad with null
unzip(unzip([[1, 2], [3, 4]])); // [[1, 2], [3, 4]] — round-trips a rectangle

What it has to do

  • Produce one column per index of the longest row.
  • Keep values in row order within each column.
  • Fill gaps from shorter rows with null.
  • Return [] for an empty input.
  • Round-trip correctly: unzipping a rectangular result twice returns the original.

Stuck? The javascript course covers everything this challenge needs.

Try it yourself
Loading playground...
5 tests · +250 XP

Discussion

Sign in to post
Sign in to join the conversation.Sign in

No comments yet. Be the first to say something.