可能重复:
如何随机化一个javascript数组?
我正在用JavaScript编写一个代码,我需要在其中获取35个输入值,在数组中为每个输入值分配一个位置,然后对它们进行洗牌,使它们以不同的顺序重新排列.因此:
var sort = new Array(35);
sort[0] = document.getElementById("d1p1").value;
sort[1] = document.getElementById("d1p2").value;
// ...
// ... (till 35)
var rand1 = Math.floor(Math.random() * 35);
var rand2 = Math.floor(Math.random() * 35);
// ...
// ... (till 35)
var rsort = new Array(35);
rsort[rand1] = document.getElementById("d1p1").value;
rsort[rand2] = document.getElementById("d1p2").value;
Run Code Online (Sandbox Code Playgroud)
唯一的问题是,因为Math.floor(Math.random()*35)从1-35多次生成一些相同的数字(好吧,我猜这是随机点),那么有时会分配两个值相同的输入框,它们返回undefined.有任何想法吗?
我正在尝试制作一个输出1-10的每个数字的脚本.在JavaScript中使用随机数生成器.
我希望每个数字都是唯一的.这是我希望脚本输出的示例:
5 9 7 6 1 3 4 8 2 10
Run Code Online (Sandbox Code Playgroud)
这是我的尝试:
var test = [];
var amountOfNumbers = 10;
var inArray = false;
var useNumbers = [];
for(var i=0; useNumbers.length<=amountOfNumbers; i++){
var rng = Math.floor((Math.random()*amountOfNumbers)+1);
for(var a=0; a<=test.length; a++){
if(rng == test[a]){
inArray == true;
}
}
if(!inArray){
document.write(rng);
test.push(rng);
useNumbers.push(rng);
}
}
Run Code Online (Sandbox Code Playgroud)
希望你能帮忙.
为了记录我对jQuery og任何其他库不感兴趣:)
我试图从数组中获取一个随机数而不重复,但使用 var newx = Math.floor(Math.random() * array.length); 确实随机化,但它是按长度而不是数组内部的内容进行的,因此它确实倾向于重复。
<html>
<head>
</head>
<!-- <body> -->
<button onclick="myFunction()">Difficulty</button>
<p id="demo">here</p>
<p id="test"></p>
<script>
function myFunction() {
function shuffle(o){ //try this shuffle function
for(var j, g, t = o.length; t; j = Math.floor(Math.random() * t), g = o[--t], o[t] = o[j], o[j] = g);
return o;
};
var i; var l; var y; var n;
var newx; var newy;
var useranswer; var amountX; var largest;
var copyAmountX;
var mixAlot;
var x = parseInt(prompt("What …Run Code Online (Sandbox Code Playgroud) 我想使用 javascript 创建 1000 个单词的虚拟文本。我的想法是使用这样的数组
var words =["The sky", "above", "the port","was", "the color of television", "tuned", "to", "a dead channel", ".", "All", "this happened", "more or less","." ,"I", "had", "the story", "bit by bit", "from various people", "and", "as generally", "happens", "in such cases", "each time", "it", "was", "a different story","." ,"It", "was", "a pleasure", "to", "burn"];
Run Code Online (Sandbox Code Playgroud)
然后使用javascript随机输出1000个单词。我将如何使用 javascript 做到这一点?还有其他方法吗?
我有这个数组:
var numberArray = [1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
使用 jQuery,以随机顺序循环遍历该数组中的所有元素一次的最简单方法是什么?有效序列为3,2,1, 2,3,1, 无效序列为1,1,1, 或2,2,3。
学习 JavaScript 并需要一些建议。我有一段简单的代码,可以在单击元素时更改元素的颜色,但我不希望在 onclick 事件上从数组中重复颜色。我将如何做到这一点或帮助学习什么是最被接受的做到这一点的方式。
尝试使用 IF 语句。
var colors = ["red", "blue", "green", "orange", "purple", "pink"];
document.querySelector('.circle').onclick = function changeColor() {
var rand = colors[Math.floor(Math.random() * colors.length)];
document.querySelector('.circle').style.color = rand;
console.log(rand);
}
Run Code Online (Sandbox Code Playgroud)
我希望颜色会改变但不会重复。
我一直在尝试以随机方式映射数组。我希望数组的每个对象都显示出来。但是刷新时顺序应该总是不同的和随机的。
const [ list ] = [
{id: 0, img: "img", key: 0, class: "wrap", classFlip: "wrap card-flip", match: false},
{id: 1, img: "img", key: 0, class: "wrap", classFlip: "wrap card-flip", match: false},
{id: 2, img: "img", key: 2, class: "wrap", classFlip: "wrap card-flip", match: false},
{id: 3, img: "img", key: 2, class: "wrap", classFlip: "wrap card-flip", match: false},
{id: 4, img: "img", key: 3, class: "wrap", classFlip: "wrap card-flip", match: false},
{id: 5, img: "img", key: 3, class: "wrap", …Run Code Online (Sandbox Code Playgroud)