如何两个数组具有相同的随机排序排序 [英] How to sort two arrays with the same random sort
问题描述
好吧,我在的jQuery / JavaScript的建立测验应用程序。
以下的小功能旨在随机的一系列的问题可能的答案,以及一系列照片。每张照片对应的答案中的一个。
在我调用此函数,这些照片和答案都在各自的包裹集中的顺序相同。
函数的确实的随机两套。但每一个被单独随机化。我需要他们都具有相同的随机化。
我无法弄清楚如何实现这一目标。我想也许可以链的jQuery他们的风格,但是这是不正确的。我也试过排序()中分离出的功能,但没有做的伎俩无论是。
谁能帮助?
函数随机化(){
。VAR elemsPhotos = $('照片')儿童('IMG')得到();
。VAR elemsQuests = $('答案')儿童('。answerLine')得到();
elemsPhotos.sort(函数(){返回(Math.round(的Math.random()) - 0.5);});
elemsQuests.sort(函数(){返回(Math.round(的Math.random()) - 0.5);});
$('照片')删除('IMG');
$('。回答')删除('。answerLine');
对于(VAR I = 0; I< elemsQuests.length;我++){
$('照片')追加(elemsPhotos [I]);
$('的答案。)追加(elemsQuests [I])。
}
}
为什么你不只是随机使用数组 N
值(其中 ñ
为题/照片的数量),并使用该数组来得到每个问题/摄阵随机指数?
VAR elemsPhotos = $('照片')儿童('IMG')得到();
。VAR elemsQuests = $('答案')儿童('。answerLine')得到();
变种N = elemsQuests.length;
变种randomIndexes = [];
对于(VAR I = 0; I< N;我++){
randomIndexes [我] =我;
}
randomIndexes.sort(函数(){返回(Math.round(的Math.random()) - 0.5);});$('照片')删除('IMG');
$('。回答')删除('。answerLine');
对于(VAR I = 0; I< N;我++){
$('照片')追加(elemsPhotos [randomIndexes [我]]);
$('。回答')追加(elemsQuests [randomIndexes [我]]);
}
Okay, I'm building a quiz application in jQuery/javascript.
The following little function is intended to randomize a series of possible answers for a question, as well as a series of photos. Each photo corresponds to one of the answers.
Before I call this function, the photos and answers are in the same order in each respective wrapped set.
The function does randomize both sets. But each one is randomized separately. I need them both to have the SAME randomization.
I can't figure out how to achieve this. I thought might be able to chain them jQuery style, but that's not right. I also tried separating out the function within the sort(), but that didn't do the trick either.
Can anyone help?
function randomize() {
var elemsPhotos = $('.photos').children('img').get();
var elemsQuests = $('.answers').children('.answerLine').get();
elemsPhotos.sort(function() { return (Math.round(Math.random())-0.5); });
elemsQuests.sort(function() { return (Math.round(Math.random())-0.5); });
$('.photos').remove('img');
$('.answers').remove('.answerLine');
for (var i=0; i < elemsQuests.length; i++) {
$('.photos').append(elemsPhotos[i]);
$('.answers').append(elemsQuests[i]);
}
}
Why don't you just randomize an array with n
values (where n
is the number of questions/photos) and use that array to get the "random" index in each question/photo array?
var elemsPhotos = $('.photos').children('img').get();
var elemsQuests = $('.answers').children('.answerLine').get();
var n = elemsQuests.length;
var randomIndexes = [];
for (var i=0; i<n; i++) {
randomIndexes[i] = i;
}
randomIndexes.sort(function() { return (Math.round(Math.random())-0.5); });
$('.photos').remove('img');
$('.answers').remove('.answerLine');
for (var i=0; i < n; i++) {
$('.photos').append(elemsPhotos[randomIndexes[i]]);
$('.answers').append(elemsQuests[randomIndexes[i]]);
}
这篇关于如何两个数组具有相同的随机排序排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!