Ask coding questions

← Back to all posts
permutations or anagrams in javascript
Yimmee

i don't really remember the definitions for either of the words in the title, but i kept looking up
"how to get every possible combination of letters in a length in javascript".
i need something else.
what can i use that takes the letters in a string and scrambles it into its' length, and allows multiple?

e.g. ab:
aa
ab
ba
bb

instead of
ab
ba

and I'm sorry i don't know what to try because i tried what has already been tried but it doesn't meet my needs and i can't try to try making one myself because it hurts my brain to think about how it works.

literally. it hurts to think sometimes.
google doesn't help and i know stackoverflow is okay in a demanding tone but i just deleted my account and it's gonna feel awkward to make a new one of the same email

Answered by MadMath123 [earned 5 cycles]
View Answer
Voters
MadMath123
Yimmee
Comments
hotnewtop
MadMath123

Let's say you had a combination lock. Each dial had the letters you wanted to use. Every word that you could make with the combination lock would be a word in your final list. If we wanted to automate it, we would turn the last dial by one. When it gets back to the start, we rotate the next dial by one. When that reaches the beginning, we turn the next, so on and so forth. Ex:
([A][A])
([A][B])
([A][C])
([A][A])
([A][B])
One more thing -- to make this program extra effective, we have to only have one copy of each letter on each dial, so that there are no repeats.
Now, let's write that. Let's define dialArr, a variable with the letters on the dial, and counter, the variable used to track the position of the dials on our combination lock.

Then, we have to set all the dials to 0.

Before I continue, I should tell you that I will use both dialArr.length and counter.length, even though they're basically the same.
Next, let's write a function to spin each dial, like I said. I'll define it as function spinNextWheel (). Let's add the code to spin the first dial by one: counter[0] ++; Right now, you can test it and see it only spins the last dial. I'll write some code that spins the next dial if the one previous gets past the limit of letters on the dial (dialArr.length).

And now, a function that tells us the letter on each dial.

Lastly, we just have to spin the dial (counter.length^dialArr.length) times to get the amount of dial spins needed to get each possible outcome.

This code is good, but not great. If there are repeats in dialArr, there'll be repeats in the console. So, before the spinNextWheel function, but after all the dials have been made, let's delete copies of letters in dialArr.

And when we run it:

Here is the WHOLE code:

MadMath123

@MadMath123 If you liked my answer, please check mark it!

Yimmee

@MadMath123 It is the answer that i wanted, but i decided that i dont need this. not many people are gonna use what i'm making anyway.

MadMath123

@Yimmee Still, if anyone comes across this, checking this makes it easier to see my answer.

Coder100

so you want to see how many unique rearrangements there are of something like CODER100?

It's easy! The formula is: length!/repeatedchars!
in the case of CODER100 we have 7!/2!=2520

now your task is to find a way to do this in js