(Note: Remarks in my first review first review about handling special cases and the returning a value still apply.)
(Note: Remarks in my first review about handling special cases and the returning a value still apply.)
(Note: Remarks in my first review about handling special cases and the returning a value still apply.)
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 forof 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");
}
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 for 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");
}
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");
}
I would like to point out that yourYour 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:
I would like to point out that 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:
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: