Ask coding questions

← Back to all posts
permutations or anagrams in javascript
Yimmee (31)

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 (341) [earned 5 cycles]
View Answer
Comments
hotnewtop
MadMath123 (341)

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.

let dialArr = ['A', 'B', 'C'];
let counter = [];

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

for(var i = 0; i < dialArr.length; i ++){
	counter.push(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).

	for(var i = 0; i < counter.length; i ++){
		if(counter[i] >= dialArr.length){ // Is the dial past the limit?
			counter[i] = 0; // Loop the dial back around
			if(i < counter.length-1){ // If this dial isn't the last one,
				counter[i + 1] ++; // add one to the next dial.
			}else{
				console.log('Success!'); // tell us we have all the permutations, because the last dial is as turned as possible without restarting the sequence
			}
		}
	}

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

function decodeArr (){
	let result = [];
	for(var i = 0; i < counter.length; i++){
		result.push(dialArr[counter[i]])
	}
	return result;
};

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.

for(var i = 0; i < Math.pow(counter.length, dialArr.length); i++){
console.log(decodeArr(counter));
spinNextWheel(counter);
}

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.

function removeRepeats (arr){
	let retArr = [];
	for(var i = 0; i < arr.length; i ++){
		if(!retArr.includes(arr[i])){
			retArr.push(arr[i]);
		}
	}
	return retArr;
};
dialArr = removeRepeats(dialArr);

And when we run it:

[ 'A', 'A', 'A' ]
[ 'B', 'A', 'A' ]
[ 'C', 'A', 'A' ]
[ 'A', 'B', 'A' ]
[ 'B', 'B', 'A' ]
[ 'C', 'B', 'A' ]
[ 'A', 'C', 'A' ]
[ 'B', 'C', 'A' ]
[ 'C', 'C', 'A' ]
[ 'A', 'A', 'B' ]
[ 'B', 'A', 'B' ]
[ 'C', 'A', 'B' ]
[ 'A', 'B', 'B' ]
[ 'B', 'B', 'B' ]
[ 'C', 'B', 'B' ]
[ 'A', 'C', 'B' ]
[ 'B', 'C', 'B' ]
[ 'C', 'C', 'B' ]
[ 'A', 'A', 'C' ]
[ 'B', 'A', 'C' ]
[ 'C', 'A', 'C' ]
[ 'A', 'B', 'C' ]
[ 'B', 'B', 'C' ]
[ 'C', 'B', 'C' ]
[ 'A', 'C', 'C' ]
[ 'B', 'C', 'C' ]
[ 'C', 'C', 'C' ]
Success!

Here is the WHOLE code:

let dialArr = ['A','B','C'];
let counter = [];
for(var i = 0; i < dialArr.length; i ++){
	counter.push(0);
}
function removeRepeats (arr){
	let retArr = [];
	for(var i = 0; i < arr.length; i ++){
		if(!retArr.includes(arr[i])){
			retArr.push(arr[i]);
		}
	}
	return retArr;
};
dialArr = removeRepeats(dialArr);
function spinNextWheel (){
	counter[0] ++;
	for(var i = 0; i < counter.length; i ++){
		if(counter[i] >= dialArr.length){
			counter[i] = 0;
			if(i < counter.length-1){
				counter[i + 1] ++;

			}else{
				console.log('Success!');
			}
		}
	}
};
function decodeArr (){
	let result = [];
	for(var i = 0; i < counter.length; i++){
		result.push(dialArr[counter[i]])
	}
	return result;
};
for(var i = 0; i < Math.pow(counter.length, dialArr.length); i++){
console.log(decodeArr(counter));
spinNextWheel(counter);
}
MadMath123 (341)

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

Yimmee (31)

@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 (341)

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

Coder100 (18057)

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