Skip to content

Instantly share code, notes, and snippets.

@TrishZwei
Last active April 4, 2022 14:39
Show Gist options
  • Save TrishZwei/e6242e807514ebcd1416 to your computer and use it in GitHub Desktop.
Save TrishZwei/e6242e807514ebcd1416 to your computer and use it in GitHub Desktop.
Array sorter for randomized order of presentations for students
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Random Student Order</title>
</head>
<body>
<h1>Order of Presentations</h1>
<p>Determined by a randomly shuffled array:</p>
<div id="message"></div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<!-- any jQuery dependent files and scripts go below jQ -->
<script>
//my custom JS code
var students = ['Denerys', 'Tyrion', 'Rob', 'Varys', 'Stannis', 'Brianne', 'Cersei'];
function shuffle(array) {
len = array.length;
//gets the initial length of the array passed to the function, tells us how many times we need to go to the array to get a new value.
var tempArray = [];
//allows us to store a set of values gotten from the initial array
for(i = 0; i<len; i++){
item = array.splice(getRandInt(0, array.length-1), 1);
tempArray.push(item[0]); //if you don't do this the item will be a one item array. This way it gets the value of the individual item that was taken out.
//console.log(tempArray);
}
return tempArray;
}
students = shuffle(students);
students = students.join('<br>');
$('#message').html(students);
//any random number Generator will do here.
//function to generate a whole random interger
//@2param: number - min, max (with defaults set)
function getRandInt(min = 1, max = 100){
return Math.floor(Math.random() *(max-min +1))+min;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment