Infinite loop? - possible combinations help

Infinite loop? - possible combinations help
0

#1

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?


#2

Inside the while loop, the following if statement condition evaluates to true everytime for find(‘ac’), so the else if statement never gets evaluated and none if it’s code gets executed, so combinations.length remains 0 forever (hence the infinite loop).


#3

I’m still confused? I thought it was just checking, and -1 means it doesn’t already contain it?


#4

On line 16 (seen below), you reset combo to a blank array, so no item is going to be in combo, so the if statement is going to always evaluate to true and execute it’s code block.

var combo = [];

#5

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');

#6

Any idea on why i get repeats?


#7

Look very carefully at the above line and you will see a ) which is in the wrong place. However, after fixing the above line, you will now have an infinite loop, because the 2nd while loop will evaluate to true forever. Why? Because the following will evaluate to true forever, because combinations.length (24) is less than or equal to possible (24).


#8

Edit got it to work, and combinations are different now. Thanks :slight_smile:


#9

When I fixed those two items, the code does not produce duplicates. Can you post your revised code in a reply?

Never mind, I see we were replying at the same time.


#10

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+" , "


#11

I am not sure what you mean by crashes.


#12

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


#13

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();
};

#14

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.