Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  • Thread Tools
  • Rate This Thread
  1. #1
    New to the CF scene
    Join Date
    May 2016
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Making a button that, on click, prints 20 possible combinations, need some help.

    Code:
    <button onclick="FFBBLLRRUUDD()">Click me</button>
    
    <p id="36"></p>
    
    <script>
    function FFBBLLRRUUDD() {
        var num = 36
        var n = num.toString();
        Math.random().toString(36).replace(/("F", "F'", "B", "B'", "L", "L'", "R", "R'", "U", "U'", "D", "D'")+/g, '').substr(0, 20).innerHTML = n;
    }

  2. #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,364
    Thanks
    52
    Thanked 531 Times in 525 Posts

    Lightbulb

    Different approach entirely.

    Note: ID values must not start with a number.

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> HTML5 page </title>
    
    </head>
    <body>
    <button onclick="document.getElementById('p36').innerHTML=FFBBLLRRUUDD()">Click me</button>
    
    <pre id="p36"></pre>
    
    <script type="text/javascript">
    function FFBBLLRRUUDD() {
      var fblrud = ['F','B','L','R','U','D'];
      var str = '', n=0;
      for (var i=0; i<20; i++) {
        n = Math.floor(Math.random()*fblrud.length);
        str += fblrud[n];
      } return str;
    }
    
    </script>
    
    </body>
    </html>
    Last edited by jmrker; 05-12-2016 at 07:13 PM.

  3. #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    4,595
    Thanks
    14
    Thanked 480 Times in 475 Posts
    see also How could i make a jscript button for a letter generator?

    Note: ID values must not start with a number.
    only before HTML5. now the only constraint is that there is no whitespace character.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  4. #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    8,131
    Thanks
    3
    Thanked 814 Times in 803 Posts
    Quote Originally Posted by Dormilich View Post
    only before HTML5. now the only constraint is that there is no whitespace character.
    of course that only guarantees that they will work in the HTML. The JavaScript standard is completely separate and so there is at least the possibility of situations where JavaScript can't handle one that HTML can.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •