jomla Posted October 19, 2017 Share Posted October 19, 2017 Hi at all, please read this function: function cc(letters, comb, m, k) { var ret=[]; if (k == 0) { ret.push(comb); } else { for (var i=m; i<= letters.length-k; ++i) { cc(letters, comb+" "+letters+' ', i+1, k-1); } } return ret; } Function cc() calc combinations of numbers or letters or any other object like words, colors ........ . to call cc(): var c=new Array(1,2,3,4,5,6,7,8,9);// it can be an array of letters or of any other object var k=3;//size of any combination that can be 2 or 3 or 4 ...... or more var start=0; demo: var r=cc(c,"",start,k); It work very well but I do not love recursion call. How can I do to obtein the some result without recursion call? Thank you very much Link to comment Share on other sites More sharing options...
justsomeguy Posted October 19, 2017 Share Posted October 19, 2017 With a long, messy series of nested loops which will end up resulting in the function only allowing a maximum size for the array or size. You should stop worrying and learn to love recursion. Link to comment Share on other sites More sharing options...
jomla Posted October 19, 2017 Author Share Posted October 19, 2017 It is not possible in an easy way with do while? I do not understand how work function cc Link to comment Share on other sites More sharing options...
justsomeguy Posted October 19, 2017 Share Posted October 19, 2017 In an easy way? No. You need nested loops, and the number of loops you write is going to set a maximum value for parts of what the function can do. Recursion is how you avoid that, you can use a much more elegant piece of code to do that kind of thing if you use recursion. The code you posted doesn't actually do anything though, it never returns a value other than an empty array when k is greater than 0. It might have gotten messed up because you didn't post it in a code box, maybe there's something left out, I'm not sure. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now