-
Notifications
You must be signed in to change notification settings - Fork 1
/
Random Entry File Loading.html
55 lines (47 loc) · 1.7 KB
/
Random Entry File Loading.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Random Entry File Loading</title>
</head>
<body>
<script src = "http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
<!--Form with button-->
<form>
<!--<button onclick="shuffle(items)">Shuffle JSON File</button>-->
<input type="button" value="Shuffle JSON File" onClick="shuffle(items)"/>
<div id="result"></div>
</form>
<script>
//global variables
document.write("Test");
//var myArray = [];
//load JSON file into array
$(getJSON("my5000Entries.json", function(data)){
var items = [];
$(each(data, function(key, val1)){
items.push("<li> <a href=#'" + key + "'>" + val1 + "> </a> </li>");
};
)};
$("<ul/>", {"class":"my-new-list", html: items.join("")}.appendTo("body");
});
//shuffle order of elements in array
//Fisher-Yates-Durstenfeld shuffle
function shuffle(sourceArray){
for (var i = 0; i < sourceArray.length - 1; i++){
//j represents random indices that are larger than current index, but can't we take both larger and smaller values??
var j = i + Math.floor(Math.random() * (sourceArray.length - i));
var temp = sourceArray[j]; //new random value
sourceArray[j] = sourceArray[i]; //switch new with old
sourceArray[i] = temp; //switch
}
return sourceArray;
}
var shuffledArr = shuffle(items);
for (int i = 0; i < shuffledArr.length; i++){
document.write(shuffledArr[i]);
}
</script>
</body>
</html>