2014-07-05

Web-adapted

Now is the time for the JavaScript version of the Hailstone program:

<script>
  function hailstone(seed) {
    var output = "";
    for ( ; 
   output += seed, seed > 1 ; 
   seed = seed % 2 ? seed * 3 + 1 : seed / 2) { output += ", "; }
    return(output);
  }

  function submit(seed) {
    document.getElementById("output").textContent =
      hailstone(seed);
  }
</script>

<p>
  Seed: <input type="text" 
            id="seed" 
            onchange="submit(this.value)">
</p>
<p id="output"></p>

Put that into an HTML page and it generates the following box: (Try it, it actually works!)

Seed:

No comments: