user3399657
user3399657

Reputation:

how to change this code to recursive form

this code can generate a string like this 'x,x,x' i want to make it in recursive form or change this code in order to make the number of x dependent on a variable Y i chose it ! eg : i put Y = 4 the script generate strings like this 'x,x,x,x'

the code

        var T = new Array ('a','b','c');
        var n = 0 , c = 1 ;
        for (var i = 0 ; i <= 2 ; i++){
            var j = 0   ;
            while (j <= n){
                for (;j <= 2 ; j++){
                    var k = 0  ;
                    while (k <= j){
                        for (;k <= 2 ; k++){
                            document.write(c + ' - ' + T[i]+' , '+ T[j] +' , '+ T[k] +' <br    /> ');
                            c++;
                        }
                    }
                }
            }

        }

the Result of this code is : https://i.sstatic.net/2c2ts.png

so i trying to make a script that can make arrangement with replacement of alphas !

Upvotes: 0

Views: 41

Answers (1)

Oriol
Oriol

Reputation: 288130

Demo

function list(target, letters, lvls) {
    var index = 0;
    (function iter(s, lvl) {
        if(lvl++ == lvls)
            return target.appendChild(document.createTextNode(
                ++index + ' - ' + s + '\n'
            ));
        for(var i=0; i<letters.length; ++i)
            iter(s + letters[i], lvl);
    })('', 0);
}
list(document.getElementById('output'), 'abc', 3);

Upvotes: 3

Related Questions