Trying to make a program that returns every possible combination of your string input.
repl.it --> https://repl.it/@John_Nicole/Possibilities
Everything is commented so you can see my thought process (infinite loop protection on --> settings (Upper left))
const find = letters => {
letters = letters.split('');
var offical = letters;
// create a set copy of that input
console.log(letters.length);
var possible = 1;
for (var i = 1; i <= letters.length; i++) {
possible *= i;
// factoraial n! for possibilties ?
}
var combinations = [];
while (combinations.length <= possible) {
// infinite loop?
console.log(combinations.length);
var combo = [];
// The current comboination of letters
var num = 0;
var item = Math.floor(Math.random() * letters.length+1);
item = letters[num];
// picks a random letter in the array of letters
if (combo.indexOf(item) === -1) {
// if the combo doesn't already contain that letter, add it in.
combo.push(item);
letters = letters.join('');
letters = letters.replace(item, '');
letters = letters.split("")
// just removes that letter from the array
} else if (combo.length === letters.length) {
// is the combo ready?
if (combinations.indexOf(combo) === -1) {
// is it not already in the array?
combinations.push(combo.join(""))
letters = offical
// resets array to starting array
combo = []
} else {
// it is a repeat!
combo = []
letters = offical
// resets array to starting array
}
}
}
return combinations;
};
find('ac');
I get two for the possibilities (console), and then a bunch of 0’s that eventually sets off infinite loop protection. I have no idea why this is happening. Spelling mistakes, can i not use that function on that item? Why a ton of 0’s?
I’m still confused? I thought it was just checking, and -1 means it doesn’t already contain it?
New code:
I thought it was working, but than i looked deeper. I noticed how i keep getting a bunch of the same combinations even though i tell it to check if that combination is already a thing.
const find = letters => {
letters = letters.split('');
var offical = letters;
var possible = 1;
for (var i = 1; i <= letters.length; i++) {
possible *= i;
}
console.log(possible +" possibilties")
var combinations = [];
var combo = []
while (combinations.length <= possible) {
// runs the loop below for each possibility
while (combo.length < offical.length) {
// all this loop does is find a random combination.
var num = Math.floor(Math.random() * letters.length)
var item = letters[num]
if (combo.indexOf(item) === -1) {
combo.push(item)
letters = letters.join("")
letters = letters.replace(item, '')
letters = letters.split("")
}
}
combo = combo.join("")
if (combinations.indexOf(combo === -1)) {
combinations.push(combo)
// trying to check if its already a thing?
}
combo = []
letters = offical
}
return combinations.sort();
};
find('face');
Any idea on why i get repeats?
Edit got it to work, and combinations are different now. Thanks
At around 4 characters, i noticed how the program just crashes. Anyway to add a wait?
My mistake was checking for combo, instead of combo+" , "
It just stops, the whole page freezes. Infinite loop protection is not on, so it will go forever until it figures it out. Only issue is, the program is doing so much, as fast as it can, for such a long period of time it ends up just freezing up and crashing (browser)
Anyway to add a 100 millisecond break?I have heard of setTimeout
When I run the following code with 4 or more letters in the string, it does not freeze. Try it to see if you still have the same problem.
const find = letters => {
letters = letters.split('');
var offical = letters;
var possible = 1;
for (var i = 1; i <= letters.length; i++) {
possible *= i;
}
console.log(possible +" possibilties")
var combinations = [];
var combo = []
while (combinations.length < possible) {
// runs the loop below for each possibility
while (combo.length < offical.length) {
// all this loop does is find a random combination.
var num = Math.floor(Math.random() * letters.length)
var item = letters[num]
if (combo.indexOf(item) === -1) {
combo.push(item)
letters = letters.join("")
letters = letters.replace(item, '')
letters = letters.split("")
}
}
combo = combo.join("")
if (combinations.indexOf(combo) === -1) {
combinations.push(combo)
// trying to check if its already a thing?
}
combo = []
letters = offical
}
return combinations.sort();
};
1 Like
Anyways how can i add a 10 millisecond wait to make it go up to even 9 characters? Currently works on 8 though.
9 = 362,880 possibilties
Edit: never mind. That would take 60.28 hours to calculate
What was causing my crash before was me console.log()ing there combinations each loop.
1 Like