5

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>

3 Answers 3

1

Here's a first fix: to add a new element to an array (like it appears you're doing in your for loop), you should use the arr.push() method. You'll also want to declare newArr as an empty array before that.

I'm not entirely sure, however, that you're doing what you intend to in your groups function. Right now it looks like you're choosing a random name from your list and then, for as many times as there are names in your list, assigning that one random name to your new array.

What might help you is to just write out a list of the steps you want to take (what we call pseudo-code), then turn that list into javascript code. It can be a lot easier to find inconsistencies in that list than it would be to sift through the sometimes confusing code.

Hope this helps, let me know if you have any more questions!

Sign up to request clarification or add additional context in comments.

5 Comments

Hi, Thanks for your help!!! Right now im having trouble finding the best way to implement what I am trying to do(im basically working with ideas and running the code to see if it works) If you could shed some light on how to actually implement what I am trying to do that would great!, Thanks
Another thing that often helps me is to work on a project a bit at a time. I'll make a small thing work (hard-coding in values to work with), then I work back from there to incorporate more functionality or work with dynamic input.
If I understand you, the core function of this is to take a list of names, and grab a certain number of random elements, is that right?
The Function of the project is to take the list of names inputted by the user and create groups with random names from the user's input. The user creates the parameters by specifying the amount of groups there are and the amount of names in each group
Okay, so the first thing I'd do is code it without the "inputted" bits. Explicitly write an array of ten or so names, and see if you can get javascript to create a random list of four of them. From there it'd be pretty easy to replace the number of names in your new array, the list of names itself, etc, with values that the user inputs. Take it in steps.
0

Not sure what is your function groups doing. As I can see it take all data in arr and put in newarr with random sequence. You should check on .push() method for creating new array. It should make your code work.

Sample code:

var newarr = ["David", "Mic"];
newarr.push("Adeline");

Result: Your newarr will have all 3 names in it.

Comments

0

This is my interpretation of your question. Hope it helps..

    <script>
    var arr = new Array();   
    function yeah()
    {
    arr = prompt("Enter your names").split(",")
    console.log(arr);
    //var random = arr[Math.floor(Math.random() * arr.length)];
    var allStudents ="";

    for(var i=0; i<arr.length; i++) {
        allStudents += arr[i] + "<br/>\n";
    }

     document.getElementById("h2").innerHTML = allStudents;
    }


    function groups()
    {  
        var arr = prompt("Enter your names").split(",");

        var groups = document.getElementById("groups").value;
        var perGrp = document.getElementById("per_grp").value;

        var innerText = "<h4>All</h4> ";
        for(var i=0; i<arr.length; i++) {
            innerText += arr[i] + ",";
        }
        innerText += "<br/>\n";            


        arr = shuffleArray(arr);

        var finalGroups = new Array();

        for(var i=0; i<groups; i++) {
            // assign shuffled elements
            var grpArr = "";
            for(j=0; j<perGrp; j++) {
                grpArr += arr[0] + ",";
                arr.shift(); // removes first element from array    
            }

            grpArr = grpArr.substring(0,grpArr.length - 1);
            finalGroups[i] = grpArr;
        }




        innerText += "<h4>Groups</h4><br/>\n";
        innerText += "<table width='100%' border='1'><tr>\n";           
        for(var i=0; i<groups; i++) {
            // print groups
            var j=i+1;
            var grpArr = finalGroups[i].split(",");
            innerText += "<td>Group " +j+"<br>";
            for(var k=0; k < grpArr.length; k++){
              innerText += grpArr[k] + "<br>";
            }

            innerText += "</td>\n";
        }
        innerText += "</tr></table>\n";

        document.getElementById("FinalGroups").innerHTML = innerText;


   } 

   /**
    * Randomize array element order in-place.
    * Using Durstenfeld shuffle algorithm.
    */
   function shuffleArray(array) {
       for (var i = array.length - 1; i > 0; i--) {
           var j = Math.floor(Math.random() * (i + 1));
           var temp = array[i];
           array[i] = array[j];
           array[j] = temp;
       }
       return array;
   }


</script>
    <style>
    /* 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: 30px; /* For lamers who don't support viewport sizing */
        font-size:10vmin;
        text-align: center;
        margin: 0;
      }
    #h2 {
        float: center;
        font-size: 10vmin;

        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;
      }


    </style>

    <div class="display">
        <div class="wrap">
            <p>Create Random Groups</p>
            <!--<div id="h2"> </div>

            <input  class="text-display" type="button" onclick="yeah()"  value="click to input data"> -->
           <!-- <input type="button" onclick="display()" value="display random"> -->
            <br>
            <input id="groups" placeholder="Enter number of Groups" >
            <input id="per_grp" placeholder="Enter number of elements per Group">
            <br/>
            <input type="button" value="Create Groups" onclick="groups()">
            <div id="FinalGroups"></div>
        </div>



    </div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.