Others have already improved the structure of the code so I'll talk about some smaller low-hanging-fruit improvements since it may not always be in your best interests to rewrite the code entirely.
Rather than chars
, I would use alphabet
since alphabet describes the purpose and contents of the string exactly.
Don't use abbreviations like arr
, it's only two characters saved and an abbreviation will take the reader longer to parse than the full word they're used to. As it is, even array
wouldn't be a good name. We can see that it returns an array, but it should be obvious what that array is intended to contain. Something like words
would be an improvement and threeLetterWords
would be better as it describes more clearly what the contents of the array is, and the intention that it is only supposed to contain three letter words.
text
is also a poor name, word
(or threeLetterWord
) would be better but there are more problems with this than just the name. Firstly, it is only used in the innermost loop so it should be declared there. This is especially important in javascript with its often confusing scoping rules. It also removes the need to reset it at the end of the loop. Additionally, its usage could be improved by replacing the three assignments with a single one, i.e. let threeLetterWord = chars[i] + chars[x] + chars[j];
. At this point I'd be tempted to remove threeLetterWord
altogether and simply use threeLetterWords.push(chars[i] + chars[x] + chars[j]);
but going to that extent can come down to personal preference.
Finally, the loop variable names are also poorly chosen. While i/j/k and x/y are common names for nested loop variables they shouldn't be mixed and where possible an actual name would improve the code, unless x/y are suitable names for the variables, for example when working with coordinates. Here I think (first|second|third)LetterIndex
would be better.
Also, don't forget your semicolons. They might not be necessary but they're definitely useful.
In the end these improvements give the following code:
const getAllPossibleThreeLetterWords = () => {
const alphabet = 'abcdefghijklmnopqrstuvwxyz';
const threeLetterWords = [];
for (let firstLetterIndex = 0; firstLetterIndex < alphabet.length; firstLetterIndex++) {
for (let secondLetterIndex = 0; secondLetterIndex < alphabet.length; secondLetterIndex++) {
for (let thirdLetterIndex = 0; thirdLetterIndex < alphabet.length; thirdLetterIndex++) {
arr.push(
alphabet[firstLetterIndex]
+ alphabet[secondLetterIndex]
+ alphabet[thirdLetterIndex]);
}
}
}
return threeLetterWords;
}
console.log(getAllPossibleThreeLetterWords());
This code still isn't as good as the other answers that use a recursive method, but it demonstrates some easy changes to make that improve the comprehensibility of the code without changing its overall structure. The only potentially risky risky change is the removal of text
but in this case references to it were easy to identify and change safely. Now that the code is easier to understand it would be easier to make more significant changes to its structure with more confidence.