mercredi 20 janvier 2016

Generating Random Groups of Elments from an Array Javascript

HI I am working on project for school and I am trying to generate random groups of names from a list of names. The group size and amount of groups will be based off the users input. I have the code to randomly generate a single element from the array, but I need some help grouping. This my code so far. Its not functioning Please help thanks

function yeah()
        {
        var arr = prompt("Enter your names").split(",")
        console.log(arr);
        var random = arr[Math.floor(Math.random() * arr.length)];
         document.getElementById("h2").innerHTML = random;
        }
       

 function groups()
       {  
            var arr = prompt("Enter your names").split(",");
            var random = arr[Math.floor(Math.random() * arr.length)];
            var numElem = document.getElementById("input2").value;
            for (i=0;i <= arr.length; i++)
            {   
                
                  
                var newArr = [random];
                console.log(newArr);
               //print name to a new list
                //remove name from old list
                /* var arr = prompt("Enter your names").split(",")
                var groupNum = document.getElementById("input1").value;
                var newArrays =  arr.length / groupNum; */
                
        
            }
            
        
       } 
  /* Reset */
  * {
        margin: 0;
        padding: 0;
  }

  html,body {
    height:100%;
    margin:0;
    font-family: 'Montserrat', 'sans-serif';
    color:#424242;
    overflow: hidden;
  }
  .display {
    text-align: center;
    display: table;
    height:100%;
    margin:0 auto;
  }
  .wrap {
    display: table-cell;
    vertical-align: middle;
  }
  p {
    font-size: 50px; /* For lamers who don't support viewport sizing */
    font-size:20vmin;
    text-align: center;
    margin: 0;
  }
#h2 {
    float: center;
    font-size: 30vmin;

    background-size: contain;

}
  input.text-display {
    display: inline-block;
    color: #5E5E5E;
    font: bold 20px arial;
    text-decoration: none;
    text-align: center;
    margin: 20px auto;
    padding: 15px 20px;
    background: #EFF0F2;
    border-radius: 4px;
    border-top: 1px solid #F5F5F5;
    box-shadow: inset 0 0 25px #E8E8E8, 0 1px 0 #C3C3C3, 0 2px 0 #C9C9C9, 0 2px 3px #333;
    text-shadow: 0px 1px 0px #F5F5F5;
  }

  span.love {
    display: block;
    position: absolute;
    bottom:10px;
    width: 100%;
    text-align: center;
  }
  span.love a {
    text-decoration: none;
    color:#D12026;
  }
  .twitter-follow-button {
    vertical-align: text-bottom;
  }
  .twitter-share-button {
    vertical-align: text-bottom;
  }
<div class="display">
            <div class="wrap">
                <p>Your random value is:</p>
                <div id="h2">Null</div>
                
                <input  class="text-display" type="button" onclick="yeah()" height="50px" width="50px" value="click to input data">
               <!-- <input type="button" onclick="display()" value="display random"> -->
                <br>
                <input id="input1" value="Enter number of Groups">
                <input id="input2" value="Enter number of elements per Group">
                <input type="button" onclick="groups()">
            </div>
            
        </div>



Aucun commentaire:

Enregistrer un commentaire