How can I tweak this Javascript code to print all permutations of sentences on each line of input?

Viewed 226

So for example if I have this input:

I am the man

I wanna get back:

I am the man
I the man
I man
I the
I am
am the man
am man
am the
the man
man
man I
man I am
etc.

This is what I have so far for the first 20 sentences of input

            for (var i = 0; i < 20; i++) {
                var currWords = data[i].DisplayName.replace(/[^\x00-\x7F]/g, "").replace(/\(/g, "").replace(/\-/g, " ").replace(/\)/g, "").replace(/\*/g, "").toLowerCase().split(/\s+/g);

                for (var j = 0; j < currWords.length; j++) {
                    var currString = "";
                    for (var k = j; k < currWords.length; k++) {
                        currString += (currWords[k] + " ");
                    }
                    console.log(currString);
                }
            }

I just want these printed so I can copy them for something else. So far it is only doing things forwards, and I cant think of an easy way to make it do every possible sentence. What do you recomend?

2 Answers
Related