Weekly Coding Challenge: Snail Sort – written in JavaScript

Posted by

2 min read ~ Hello readers! It’s really important for me to continue my practice and learning. Lately, I have been completing a coding challenge a week with codewars. This helps me keep my skills sharp and see how other devs all over the planet would solve these problems. Being able to see other solutions expands my mind for solving future problems in a clever and efficient way. Today, the problem set focuses on JavaScript.

Snail Sort

Given an n x n array, return the array elements arranged from outermost elements to the middle element, traveling clockwise.

array = [[1,2,3],
         [4,5,6],
         [7,8,9]]
snail(array) #=> [1,2,3,6,9,8,7,4,5]

For better understanding, please follow the numbers of the next array consecutively:

array = [[1,2,3],
         [8,9,4],
         [7,6,5]]
snail(array) #=> [1,2,3,4,5,6,7,8,9]

This image will illustrate things more clearly:

snail = function(array) {
  let finalArr = []
  
  while (array.length !== 0) {
    // Step one is to get the top section of the array of arrays
    finalArr.push(...stepOne(array));
    // Step two is to get the right hand vertical side of the square
    finalArr.push(...stepTwo(array));
    // Step three is to get the bottom layer
    finalArr.push(...stepThree(array));
    // Step four is to get the left hand vertical side of the square
    finalArr.push(...stepFour(array));
    // After each of the iterations, the step removes the designated section that it just iterated over and evaluated
  }
  return finalArr;
}

stepOne = function(arr) {
  let snail = arr[0];
  arr.shift()
  return snail
}

stepTwo = function(arr) {
  let snail = [];
  arr.forEach(group => {
    snail.push(group.pop())
  })
  return snail;
}

stepThree = function(arr) {
  
  let clonedArr = [...arr];
  arr.pop()
  return clonedArr.length !== 0 ? clonedArr[clonedArr.length - 1].reverse() : []
}

stepFour = function(arr) {
  let snail = []
  if (arr.length > 1) {
    arr.forEach(group => {
      snail.push(group.shift())
    })
    snail.reverse()
  } else if (arr.length === 1) {
    snail.push(...arr[0])
    arr.shift()
  }
  return snail;
}

Disclaimer: *Now, as much as I am tempted to use ES8 & ES9 functions to reduce the number of lines in JavaScript, I am always a fan of readable code that others can understand. Therefore, with the JavaScript examples, I will try to write it without fancy functions as much as possible so it can actually be read and absorbed by my readers. All of my solutions are commented to explain my thinking. Of course, they could always be better. Please feel free to share how you would solve it!*

All solutions are also now on my GitHub profile if you want to check out all of the coding challenges I’ve completed so far!

All credit to problem sets goes to codewars

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s