Not sure, it works fine when I run it in my browser (Chrome 66). Maybe you’re running it on a platform that does not support ES6 yet, as .forEach is part of ES6 and not supported in ES5.
Well, that’s because allanagrams returns an object, {'d': true} for example, and .forEach does not work on objects. Object.keys takes all keys from the object and returns an array, so {'d': true} becomes ['d'], and .forEach does work on arrays it works when you add Object.keys.
Code like this is just hard to understand, I can’t write any pseudo code that will make it easier to understand.
I suggest you add console.log to check on several points what’s going on.
It attempts to derive the set of permutations of a string.
By definition, this is an array of strings, with the size of the array being n!, and n being the length of the original string.
This gets a bit ridiculous after about n = 10. (ever wonder why password requirements often require a certain length? Here’s why.)
The general theory would be:
For each letter of the string, consume that letter and add it to the output string.
Then take the remainder of the string, and repeat the process, until you consume all of the letters.
Combine all of the letters you’ve consumed.
All possible arrangements of a given set of characters (Note that this is not a Set) would be the list of anagrams.
Just wondering, why not? I mean, it doesn’t contain any duplicates, so in my mind it does adhere to the definition of a set, which according to Wikipedia is:
A set is a well-defined collection of distinct objects.
“theme”.
In character terms, the Set of characters in that word is {t,h,e,m}. You can construct 5 letter words from that set, but you’d only create an ANAGRAM of the original word if the 5th letter selected was a second e.
Properly, the collection of objects we work with for “theme” is a set of 5 character objects, 2 of which have the same character data, but are distinct.