Unravelling the Matrix Array.

Rules of the Challenge

// Sample Array (4 x 4 matrix)
// 1 2 3 4
// 12 13 14 5
// 11 16 15 6
// 10 9 8 7
matrix = [[1, 2, 3, 4], [12, 13, 14, 5], [11, 16, 15, 6], [10, 9, 8, 7]]// expected output
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]

Step 1

Step 2

function unrollMatrix(matrix){    // grab top row    // grab right column    // grab bottom row and reverse it    // grab left column and reverse it}

Step 3

function unrollMatrix(matrix){   let unrolled = []// grab top row   unrolled = [...unrolled, matrix.shift()]// grab right column// grab bottom row and reverse it// grab left column and reverse it}

Step 4

function unrollMatrix(matrix){    let unrolled = []// grab top row    unrolled = [...unrolled, matrix.shift()]// grab right column    unrolled = [...unrolled, matrix.map(row => row.pop())]// grab bottom row and reverse it// grab left column and reverse it}

Step 5

function unrollMatrix(matrix){    let unrolled = []// grab top row    unrolled = [...unrolled, matrix.shift()]// grab right column    unrolled = [...unrolled, matrix.map(row => row.pop())]// grab bottom row and reverse it
unrolled = [...unrolled, matrix.pop().reverse()]
// grab left column and reverse it}

Step 6

function unrollMatrix(matrix){    let unrolled = []// grab top row    unrolled = [...unrolled, matrix.shift()]// grab right column    unrolled = [...unrolled, matrix.map(row => row.pop())]// grab bottom row and reverse it    unrolled = [...unrolled, matrix.pop().reverse()]// grab left column and reverse it     unrolled = [...unrolled, matrix.map(row => row.shift()).reverse()]}

Step 7

function unrollMatrix(matrix){let unrolled = []while (matrix.length > 0) {// grab top row    unrolled = [...unrolled, matrix.shift()]// grab right column    unrolled = [...unrolled, matrix.map(row => row.pop())]// grab bottom row and reverse it    unrolled = [...unrolled, matrix.pop().reverse()]// grab left column and reverse it    unrolled = [...unrolled, matrix.map(row => row.shift()).reverse()]
}
return unrolled.flat()}

--

--

--

Fullstack Software Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Back-End pagination with NodeJS, ExpressJS, MongoDB, Mongoose, EJS

Angular and Rails API — presigned URLs for S3 image uploading

JavaScript Project Flatiron Phase 4

IIFF aka Immediately Invoked Function in JavaScript

React — Generating dynamic components

Schedule postman collection run in Node.js

Display Role Hierarchy using Lightning Tree

React Application Hierarchy

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alex Duterte

Alex Duterte

Fullstack Software Engineer

More from Medium

Coding — The Artist’s Unusual Ally

The Window Object and var

Algorithms Explained Fast and Clear (No BS)

Breaking down the Callback Function