lundi 11 février 2019

how to create multiple arrays of random numbers for javascript data-randomizer?

I am building a data randomizer in javascript for a history class which I teach. I want a user to enter into a form number of students, number of weeks, and number of assignmnets per each student and submit this data by clicking a button. For example 40 students, 12 weeks, 5 assignments for each student. Accordingly, I want the program to generate a table of 40 rows each containing 5 random unique numbers from 1 to 12. I managed to make the program generate one row of random numbers (function weeksAssigned), but then I got stuck. How do I repeat function weeksAssigned to produce other rows? I was trying to use two dimensional arrays, but I don't know how to randomize each row inside the 2d array. I will be very grateful for your suggestions! I guess I should somehow utilize either a for loop or 2d arrays, but I am not sure how.

function getRandomWeek() {
var x,y;
x=document.getElementById("form1").elements["duration"].value; 
y=Math.floor(Math.random() * x + 1);
return y;
} 

function weeksAssigned() {
var ints = [];
var assignments=document.getElementById("form1").elements["assignments"].value; 
while (ints.length < assignments) {                                                    
var randomWeak = getRandomWeek();
if(ints.indexOf(randomWeak)===-1){   
  ints.push(randomWeak);
    }
 } 
ints.sort(function(a, b){return a-b});
return ints;

}




Aucun commentaire:

Enregistrer un commentaire