Skip to content

Instantly share code, notes, and snippets.

@myy
Created April 26, 2013 09:55
Show Gist options
  • Save myy/5466180 to your computer and use it in GitHub Desktop.
Save myy/5466180 to your computer and use it in GitHub Desktop.
1からNまでの整数を並べ替えるHTML版.
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>1からNまでの整数をランダムに並び替える</title>
</head>
<body>
<h1>1からNまでの整数をランダムに並び替える</h1>
<p>並び替えたい数の最大値を入力してください: <input type="text" name="max" id="max" value="0" /> <input type="button" value="決定" onclick="getMax();" /></p>
<p><input type="button" value="並び替える" onclick="randOrder();"/></p>
<p>並び替え前: <span id="before"></span></p>
<p>並び替え後: <span id="after"></span></p>
<script>
var before;
var after;
function getMax(){
var max = document.getElementById('max').value;
before = new Array(max);
after = new Array(max);
for(var i=0;i<max;i++) {
before[i] = i+1;
}
document.getElementById('before').innerHTML = before;
}
function randOrder() {
var j = 0;
while(before.length > 0) {
var r = Math.floor(Math.random() * before.length);
after[j] = before[r];
before.splice(r,1);
j++;
}
document.getElementById('after').innerHTML = after;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment