{"ScriptPreparationCode":"var\r\n input = \u0027There was no possibility of taking a walk that day. We had been wandering, indeed, in the leafless shrubbery an hour in the morning; but since dinner (Mrs. Reed, when there was no company, dined early) the cold winter wind had brought with it clouds so sombre, and a rain so\u0027;","TestCases":[{"Name":"my answer","Code":"function splitText(text, maxLength = 30) {\r\n const result = [];\r\n while (text !== \u0027\u0027) {\r\n let \r\n endIndex; \r\n if (maxLength \u003E text.length) { // [A]\r\n endIndex = text.length;\r\n } else if (text.substring(maxLength, 1) !== \u0027 \u0027) { // [B}\r\n endIndex = text.lastIndexOf(\u0027 \u0027, maxLength);\r\n } else {\r\n endIndex = maxLength;\r\n }\r\n result.push(text.substring(0, endIndex));\r\n text = text.substr(endIndex).trim();\r\n }\r\n return result;\r\n}\r\nconsole.log(splitText(input));","IsDeferred":false},{"Name":"my answer non-destructive","Code":"function splitText(text, maxLength = 30) {\r\n const result = [], ubound = text.length;\r\n let currentPos = 0;\r\n debugger;\r\n while (currentPos \u003C ubound) {\r\n let \r\n endIndex = currentPos \u002B maxLength; \r\n if (endIndex \u003E ubound) { // [A]\r\n endIndex = text.length;\r\n } else if (text.substring(endIndex, 1) !== \u0027 \u0027) { // [B}\r\n endIndex = text.lastIndexOf(\u0027 \u0027, endIndex) \u002B 1;\r\n }\r\n result.push(text.substring(currentPos, endIndex).trim());\r\n\t\tcurrentPos = endIndex;\r\n }\r\n return result;\r\n}\r\nconsole.log(splitText(input));","IsDeferred":false}]}