Decode the Slanted Ciphertext LeetCode solution in JavaScript
1var decodeCiphertext = function (encodedText, rows) {2const numColumns = encodedText.length / rows;3const stringBuilder = [];4let nextCol = 1;5let row = 0;6let col = 0;7let index = 0;8while (index < encodedText.length) {9stringBuilder.push(encodedText[index]);10if (row === rows - 1 || col === numColumns - 1) {11row = 0;12col = nextCol;13nextCol++;14} else {15row++;16col++;17}18index = calcIndex(row, col, numColumns);19}20while (stringBuilder[stringBuilder.length - 1] === " ") {21stringBuilder.pop();22}23return stringBuilder.join("");24};2526function calcIndex(row, col, numColumns) {27return row * numColumns + col;28}