Home > Web Front-end > JS Tutorial > body text

JavaScript black hole number calculation route search algorithm (recursive algorithm) example_javascript skills

WBOY
Release: 2016-05-16 15:17:21
Original
1666 people have browsed it

The example in this article describes the JavaScript black hole number calculation route search algorithm. 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 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>运算路线查找算法</title>
  <script type="text/javascript" >
  var BLACKHOLENMB = 6174;
  var count = 0;
  function blackHole(nmb) {
    if (isNaN(nmb) || nmb < 1000 || nmb > 9999) {
      return -1;
    }
    count++;
    var tempArray = (nmb + "").split("");
    var smallNmb = parseInt(tempArray.sort().join(""));
    var bigNmb = parseInt(tempArray.reverse().join(""));
    var d_value = bigNmb - smallNmb;
    log(bigNmb, smallNmb, d_value, count);
    if (d_value != BLACKHOLENMB) {
      return blackHole(d_value);
    } else {
      return count;
    }
  }
  function log(big, small, d_value, count) {
    console.log("step " + count + ":" + big + "-" + small + "=" + d_value);
  }
  console.log(blackHole(2167));
  </script>
</head>
<body>
</body>
</html>

Copy after login

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

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template