The transpose exercise asks a user to transpose a given text input: Transpose in JavaScript on Exercism
Some solutions only trim the last line insead of all the lines. I opened this Pull Request to test those edge cases: Transpose: ensure all lines are right-trimmed by homersimpsons · Pull Request #2198 · exercism/problem-specifications · GitHub.
For context I had the following solution (in JavaScript) that was just trimming the last line of the input. The mentee was aware enough to find those failing cases
export const transpose = (input) => {
if (input.length == 0) return input;
const rows = input.length;
// Find the length of the longest string
const columns = Math.max(...input.map((arr) => arr.length));
// Now pad the smaller string to make them all equal in length
input = input.map((str) => str.padEnd(columns));
let output = Array(columns)
.fill()
.map(() => Array(rows).fill(''));
for (let i = 0; i < rows; i++) {
for (let j = 0; j < columns; j++) {
output[j][i] = input[i][j];
}
}
output = output.map((arr) => arr.join(''));
// remove the right padding at the last item, but can't it occur in the last 2 rows?
output[output.length - 1] = output[output.length - 1].trimEnd();
return output;
};