Per a Reddit Daily Programming Challenge, I am performing a matrix transposition on a string using JavaScript.Example input: 'The quick brown fox jumped over the lazy dog'Output:Tqbfjotldhurouvhaoeioxmeezg cw pr y kn e    dWhile my code works, I wanted to know:From a readability standpoint, would it be better to break the code up into smaller functions?If there's anything in the code that can be simplified.transposeString('The quick brown fox jumped over the lazy dog');function transposeString(str) {  if (str.indexOf(' ') === -1) {    var map = Array.prototype.map;    var output = map.call(str, function (char) {        return char + '\n';    });    return output.join('');  } else {    var master = [],        longestWord = '';    words = str.split(' ');    words.forEach(function(word) {      if (word.length >= longestWord.length) longestWord = word;    });    longestWord.split('').forEach(function() {      master.push([]);    });    var treatedWords = words.map(function (word) {      if (word.length < longestWord.length) {        var spaces = Array(longestWord.length - word.length + 1).join(' ');        return word = word + spaces;      } else {        return word;      }    });    treatedWords.forEach(function (word) {      var n = 0;      word.split('').forEach(function (char) {        master[n].push(char);        n++;      });    });    master.forEach(function (arr) {      console.log(arr.join(''));    });    return;  }}
Matrix transposition of a string
javascript;strings;matrix
Since you asked if the code could be simplified or made more readable, let's explore other programming styles.(Note: Remarks in my first review about handling special cases and the returning a value still apply.)First, I would like to point out that a bland solution, building the output one character at a time, would actually be slightly shorter than your original:function transposeString(str) {    if (str == null) return str;    var words = str.split(' ');    var out = [];    for (var c = 0; ; c++) {        var line = [];        var done = true;        for (var w = 0; w < words.length; w++) {            if (c < words[w].length) {                done = false;                line.push(words[w].charAt(c));            } else {                line.push(' ');            }        }        if (done) {            break;        } else {            out.push(line.join(''));        }    }    return out.join(\n);}Your solution feels a little like functional programming, though not quite.  What if you go all the way with functional programming?  Your solution was already quite close; you just need to replace .forEach() with .map().  Here's how it could look:function transposeString(str) {    if (str == null) return str;    // Wrapper for String.length    function length(word) { return word.length; };    // range(3) -> [0, 1, 2]    // http://stackoverflow.com/a/11128802/1157100    function range(n) {        return Array(n).join().split(',').map(function(e, i) { return i; });    };    var words = str.split(' ');    var maxWordLength = Math.max.apply(null, words.map(length));    // These functions could be anonymous; I've named them for clarity    return range(maxWordLength).map(function outputLineUsingNthChar(n) {        return words.map(function nthCharWithSpacePadding(word) {            return return n < word.length ? word.charAt(n) : ' ';        }).join('');    }).join(\n);}That is already shorter, even though JavaScript makes max(words.length) and range() awkward.  If you use a library like Underscore.js to provide support for max() and range(), it could be very compact.  I'd take advantage of that compactness as an opportunity to comment generously.function transposeString(str) {    if (str == null) return str;    var words = str.split(' ');    var maxWordLength = _.max(_.pluck(words, 'length'));    return _.range(maxWordLength).map(        // Generate a line of output using the nth character of each word        function outputLineUsingNthChar(n) {            return words.map(                // Extract the nth character of a word, space-padded if needed                function nthCharWithSpacePadding(word) {                    return n < word.length ? word.charAt(n) : ' ';                }            ).join('');        }    ).join(\n);}