I was thinking of a few other ways:
One way is to do it in reverse, where you just chopping off the last character after each line and then display the lines in reverse:
var letters = "ABCD3FGHIJKLMNOPQRSTUVWXYZ";
var list = [];
var result = document.getElementById("result");
while (letters.length > 0) {
list.push(letters + "<br>");
letters = letters.substring(0, letters.length - 1);
}
.innerHTML = list.reverse();
We could use the reduce method to achieve the same thing too:
var letters = "ABCD3FGHIJKLMNOPQRSTUVWXYZ";
var result = document.getElementById('result');
result.innerHTML = letters.split("").reduce(function(result) {
result.push(letters + "<br>");
letters = letters.substring(0, letters.length - 1);
return result;
}, []).reverse().join("")
The only problem with these techniques though is that it destroys the letters string. It’s better if we don’t have such side-effects.
Instead of destroying the letters, we can recreate them by joining up the split array instead. And because the reduce function gives us among other things an index number and the array of chars, we can do it in forward order handily exploiting that index to our benefit:
var letters = "ABCD3FGHIJKLMNOPQRSTUVWXYZ";
var result = document.getElementById('result');
result.innerHTML = letters.split("").reduce(function(result, char, index, arr) {
var chars = arr.join("");
result.push(chars.slice(0, index + 1) + "<br>");
return result;
}, []).join("");
Which brings us back to Felgall’s solution, where I present it again but with variable names to help aid with the understanding:
var letters = "ABCD3FGHIJKLMNOPQRSTUVWXYZ";
var result = document.getElementById("result");
result.innerHTML = letters.split("").map(function(char, index, arr) {
var allChars = arr.join("");
var chars = allChars.substr(0, index + 1);
return chars + "<br>";
}).join("");
Hopefully being expanded out by clear variable names, the behaviour of the code will be easier to comprehend, allowing a clearer understanding of how the code with less variables behaves:
document.getElementById("result").innerHTML =
"ABCD3FGHIJKLMNOPQRSTUVWXYZ".split("").map(function(char, index, arr) {
return arr.join("").substr(0, index + 1) + "<br>";
}).join("");