Javascript: Snail

JS: Arrays
Last update: 29 Apr 11:14
0
Students

We can represent the matrix with a two-dimensional array. For instance, [[1, 2, 3], [4, 5, 6], [7, 8, 9]] is an array representing the following matrix:

1 2 3
4 5 6
7 8 9

snail.js

Write and export as default a function that takes a matrix and returns an array of matrix elements in order from the top left element clockwise to the inner element. The movement across the matrix resembles a pattern on the snail shell:

Путь улитки Snail path

Examples:

buildSnailPath([
  [1, 2],
  [3, 4],
]); // [1, 2, 4, 3]

buildSnailPath([
  [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9, 10, 11, 12],
]) // [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] 

Tips

  • You can use lodash library to solve this task

For full access to the challenge you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
900
exercises
2000+
hours of theory
3200
tests