Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

spiral traversal clockwise direction js

var input = [[1,  2,   3,  4],
             [5,  6,   7,  8],
             [9,  10, 11, 12],
             [13, 14, 15, 16]];

var spiralTraversal = function(matriks){
  var result = [];
    var goAround = function(matrix) {
        if (matrix.length == 0) {
            return;
        }

        // right
        result = result.concat(matrix.shift());

        // down
        for (var j=1; j < matrix.length - 1; j++) {
            result.push(matrix[j].pop());
        }

        // bottom
        result = result.concat(matrix.pop().reverse());

        // up
        for (var k=matrix.length -2; k > 0; k--) {
            result.push(matrix[k].shift());
        }

        return goAround(matrix);
    };

    goAround(matriks);

    return result;
};
var result = spiralTraversal(input);

console.log('result', result);
Comment

PREVIOUS NEXT
Code Example
Javascript :: marko js 
Javascript :: HSETNX in redis 
Javascript :: javascript list all elements in set 
Javascript :: get elements by class name wildcard 
Javascript :: nodelist example 
Javascript :: decimal to hex 
Javascript :: how to put multiple conditions in if statement node .js 
Javascript :: js function expression 
Javascript :: ternary operators js 
Javascript :: add line break in innerhtml 
Javascript :: javascript brightness filter 
Javascript :: array sort 
Javascript :: react native icons 
Javascript :: hide and show div using javascript with example 
Javascript :: javascript load content from file 
Javascript :: react native scrollview item bottom 
Javascript :: Lazy Loading 
Javascript :: get element by id angular 
Javascript :: donwload data from react js in json file 
Javascript :: java script removing first three indexes 
Javascript :: delegate in javascript 
Javascript :: react native loop in render 
Javascript :: rest operator in javascript 
Javascript :: is there an api for netflix shows 
Javascript :: code cat 
Javascript :: to htmlhow can i add the list in javascript 
Javascript :: && in javascript 
Javascript :: javascript prompt on window close 
Javascript :: even numbers in an array 
Javascript :: use of parse in react 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =