JavaScript 上的不同随机字符串锦标赛匹配
Diffrent Random String Tournament Matching on JavaScript
需要在JavaScript中用外部输入的字符串创建一个Array,并以2×2的方式打印出来,前提是串联的Array不同。我试过这段代码,但随机数是一样的。帮不上忙。我应该将输入与数组匹配。
<!DOCTYPE html>
<html>
<head>
<script>
function isim(){
var myArray = ["Name1", 'Name2', 'Name3', 'Name4'];
var rand = myArray[(Math.random() * myArray.length) | 0]
var rand2 = myArray[(Math.random() * myArray.length) | 0]
var rand3 = myArray[(Math.random() * myArray.length) | 0]
var rand4 = myArray[(Math.random() * myArray.length) | 0]
document.getElementById("sonuc1").innerHTML = rand;
document.getElementById("sonuc2").innerHTML = rand2;
document.getElementById("sonuc3").innerHTML = rand3;
document.getElementById("sonuc4").innerHTML = rand4;
}
</script>
</head>
<body>
<center>
<h1>Match</h1>
<form>
<input name="isim[1]" placeholder="Name1"> <br><br>
<input name="isim[2]" placeholder="Name2"><br><br>
<input name="isim[3]" placeholder="Name3"><br><br>
<input name="isim[4]" placeholder="Name4"><br><br>
<input type="button" value="Match" onclick="isim()">
</form>
<p id="sonuc1"></p>
<p id="sonuc2"></p>
<br><br>
<p id="sonuc3"></p>
<p id="sonuc4"></p>
</center>
</body>
</html>
如果您每次都只是 select 数组中的随机项目,则可以多次选择相同的数组条目。为确保 rand1 到 rand4 是唯一的,您必须从数组中删除已拾取的项目,或防止通过其他方式拾取它。一种方法是随机排列数组和 pop
或 shift
您的随机条目,如下所示:
<!DOCTYPE html>
<html>
<head>
<script>
function shuffle(a) {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
function isim(){
var myArray = ["Name1", 'Name2', 'Name3', 'Name4'];
shuffle(myArray);
var rand = myArray.pop();
var rand2 = myArray.pop();
var rand3 = myArray.pop();
var rand4 = myArray.pop();
document.getElementById("sonuc1").innerHTML = rand;
document.getElementById("sonuc2").innerHTML = rand2;
document.getElementById("sonuc3").innerHTML = rand3;
document.getElementById("sonuc4").innerHTML = rand4;
}
</script>
</head>
<body>
<center>
<h1>Match</h1>
<form>
<input name="isim[1]" placeholder="Name1"> <br><br>
<input name="isim[2]" placeholder="Name2"><br><br>
<input name="isim[3]" placeholder="Name3"><br><br>
<input name="isim[4]" placeholder="Name4"><br><br>
<input type="button" value="Match" onclick="isim()">
</form>
<p id="sonuc1"></p>
<p id="sonuc2"></p>
<br><br>
<p id="sonuc3"></p>
<p id="sonuc4"></p>
</center>
</body>
</html>
需要在JavaScript中用外部输入的字符串创建一个Array,并以2×2的方式打印出来,前提是串联的Array不同。我试过这段代码,但随机数是一样的。帮不上忙。我应该将输入与数组匹配。
<!DOCTYPE html>
<html>
<head>
<script>
function isim(){
var myArray = ["Name1", 'Name2', 'Name3', 'Name4'];
var rand = myArray[(Math.random() * myArray.length) | 0]
var rand2 = myArray[(Math.random() * myArray.length) | 0]
var rand3 = myArray[(Math.random() * myArray.length) | 0]
var rand4 = myArray[(Math.random() * myArray.length) | 0]
document.getElementById("sonuc1").innerHTML = rand;
document.getElementById("sonuc2").innerHTML = rand2;
document.getElementById("sonuc3").innerHTML = rand3;
document.getElementById("sonuc4").innerHTML = rand4;
}
</script>
</head>
<body>
<center>
<h1>Match</h1>
<form>
<input name="isim[1]" placeholder="Name1"> <br><br>
<input name="isim[2]" placeholder="Name2"><br><br>
<input name="isim[3]" placeholder="Name3"><br><br>
<input name="isim[4]" placeholder="Name4"><br><br>
<input type="button" value="Match" onclick="isim()">
</form>
<p id="sonuc1"></p>
<p id="sonuc2"></p>
<br><br>
<p id="sonuc3"></p>
<p id="sonuc4"></p>
</center>
</body>
</html>
如果您每次都只是 select 数组中的随机项目,则可以多次选择相同的数组条目。为确保 rand1 到 rand4 是唯一的,您必须从数组中删除已拾取的项目,或防止通过其他方式拾取它。一种方法是随机排列数组和 pop
或 shift
您的随机条目,如下所示:
<!DOCTYPE html>
<html>
<head>
<script>
function shuffle(a) {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
function isim(){
var myArray = ["Name1", 'Name2', 'Name3', 'Name4'];
shuffle(myArray);
var rand = myArray.pop();
var rand2 = myArray.pop();
var rand3 = myArray.pop();
var rand4 = myArray.pop();
document.getElementById("sonuc1").innerHTML = rand;
document.getElementById("sonuc2").innerHTML = rand2;
document.getElementById("sonuc3").innerHTML = rand3;
document.getElementById("sonuc4").innerHTML = rand4;
}
</script>
</head>
<body>
<center>
<h1>Match</h1>
<form>
<input name="isim[1]" placeholder="Name1"> <br><br>
<input name="isim[2]" placeholder="Name2"><br><br>
<input name="isim[3]" placeholder="Name3"><br><br>
<input name="isim[4]" placeholder="Name4"><br><br>
<input type="button" value="Match" onclick="isim()">
</form>
<p id="sonuc1"></p>
<p id="sonuc2"></p>
<br><br>
<p id="sonuc3"></p>
<p id="sonuc4"></p>
</center>
</body>
</html>