Anagrams generator in javascript?

When i try to return result object it showing me following error allanagrams(…).forEach is not a function .why is that?

function allanagrams(string){

 if(string.length === 0) return [''];
   var result = {};
   string.split('').forEach(function(letter,i){
         var remainingLetters = string.slice(0,i) + string.slice(i+1);

          allanagrams(remainingLetters).forEach(
              function(anagram){
          result[letter+anagram] = true;
});
});
       return Object.keys(result);
       
}

var str = 'abc';
console.log(allanagrams(str));

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.

Above code runs fine. instead of returning Object.keys(result) if u return just result it throwing error.

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.

Makes sense?

1 Like

will you just give me the pseudocode for above code bcoz it is hard to understand?

You wrote the code, what don’t you understand? :slight_smile:

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.

1 Like

that not my code i took it from the internet.

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.

2 Likes

got it…

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.

https://en.m.wikipedia.org/wiki/Set_(mathematics)

It depends a bit on what you declare ‘distinct’.

“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.

Ah, you’re talking about the initial word. I thought you were talking about the resulting anagrams.

It all makes sense now :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.