3 Replies Latest reply on Oct 31, 2015 8:23 AM by alexa68671544

    Selecting items from an array multiple times

    alexa68671544

      Hi. I have an array that contains all the letters of the English alphabet plus a full stop and a space. What I wanted to do is to select randomly a large number of these letters and print them on the screen. My code this far is as follows:

       

      <script type="text/javascript">

       

        var letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L","M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", ".", " "];

       

         for (var i=0; i<=10; i++){

       

              var random_letter = Math.floor(Math.random()*28);

       

              var result=[];

       

              result[i] = [letters[random_letter]];

       

              document.write(result);

         }

       

      </script>

       

      When I run the above screen in Firefox I get this output:

       

      X,C,,N,,,A,,,,T,,,,,.,,,,,,N,,,,,,,T,,,,,,,,G,,,,,,,,,O,,,,,,,,,,G

       

      which includes 10 letters and a space. But, it also includes all those commas. Why is this happening? Am I doing something wrong with the code? Any help will be much appreciated.

      Alex