Home  >  Article  >  Web Front-end  >  js method to implement simple permutation and combination_javascript skills

js method to implement simple permutation and combination_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:17:381302browse

The example in this article describes how to implement simple permutation and combination in js. Share it with everyone for your reference, the details are as follows:

The screenshot of the running effect is as follows:

The specific code is as follows:

<!DOCTYPE html>
<html>
 <head>
  <title>demo</title>
  <script type="text/javascript">
   var str = [1,2,3,4,5];
   var count = 0;
   function arrange(s){
    for(var i=0,length=str.length; i<length; i++) {
     if(s.length == length - 1) {
      if(s.indexOf(str[i]) < 0) {
       count++;
       console.log("组合"+count+"="+s + str[i]);
      }
      continue;
     }
     if(s.indexOf(str[i]) < 0) {
      arrange(s+str[i]);
     }
    }
   }
   arrange("");
  </script>
 </head>
 <body>
 </body>
</html>

Readers who are interested in more content related to JavaScript algorithms can check out the special topics on this site: "Summary of JavaScript mathematical operation usage", "Summary of JavaScript sorting algorithms", "Summary of JavaScript traversal algorithms and techniques" and "Summary of JavaScript data structures and algorithm techniques"

I hope this article will be helpful to everyone in JavaScript programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn