Skip to content

Instantly share code, notes, and snippets.

@finalcut
Created February 4, 2019 22:16
Show Gist options
  • Save finalcut/9f45d4ba00768f912662137609fb9fd7 to your computer and use it in GitHub Desktop.
Save finalcut/9f45d4ba00768f912662137609fb9fd7 to your computer and use it in GitHub Desktop.
randomly assign a task to a random person
<html>
<head>
<script type="text/javascript">
// random number will return
// an integer (whole number) between 0 and maxValue-1
// so, if you have an array of six elements and you want
// a random index value to find a value in that array
// you'd pass in the array length of six and this would
// return a value between 0 and 5
function randomNumber(maxValue){
return Math.floor(Math.random() * maxValue);
}
function removeFromArrayAtIndex(myArray,index){
// we need to know how long the myArray array is:
var lastIndex = myArray.length -1;
if(index === 0){
// slice with one argument will get from
// the index specified all the way to the end
myArray = array.slice(1);
}
// removing last person, so we grab from the first
//to the next to last
else if(index === lastIndex){
myArray = myArray.slice(0, lastIndex-1);
}
else {
// removing someone from the middle of the array
// slice will go from the first argument index
// and then count out to the second argument number
// of indexes to get data.
var firstPart = myArray.slice(0,index);
// slice with only one argument will start at the first
// index and then grab all the way to the end of the array
var lastPart = myArray.slice(index+1);
// concat is an array method to concatenate two arrays into
// one resulting array.
myArray = firstPart.concat(lastPart);
}
return myArray;
}
var students = [];
students.push('Jared Adkins');
students.push('Lindsey Brumfield');
students.push('Danielle Goodman');
students.push('Heather Hogsett');
students.push('Jacob Insco');
students.push('Hunter Johnson');
students.push('Jennifer Kubeck');
students.push('Michael Laxton');
students.push('Mason Liu');
students.push('Brianna Pruitt');
students.push('Lauren Sharp');
var assignments = [];
assignments.push('Angular');
assignments.push('Vue');
assignments.push('React');
var studentIndex = randomNumber(students.length);
var assignmentIndex = randomNumber(assignments.length);
var student = students[studentIndex];
students = removeFromArrayAtIndex(students,studentIndex);
var assignment = assignments[assignmentIndex];
assignments = removeFromArrayAtIndex(assignments, assignmentIndex);
console.log(student);
console.log(assignment);
console.log(assignments);
</script>
</head>
<body>
this is a page for randomly selecting students for research work
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment