function randomSelect (count, sourceArray, destinationArray) {
if (destinationArray.length >= count) {
// the "base case", used to break out of recursion
return destinationArray;
}
var randomIndex = Math.floor(Math.random() * sourceArray.length);
var item = sourceArray[randomIndex];
if (destinationArray.indexOf(item) < 0) {
// call "randomSelect" again with the item added to the destination (recurse)
return randomSelect(count, sourceArray, destinationArray.concat(item));
}
// call "randomSelect" again (recurse)
return randomSelect(count, sourceArray, destinationArray);
}
console.log(randomSelect(2, [1, 2, 3, 4, 5], []));
console.log(randomSelect(2, ['a', 'b', 'c', 'd', 'e'], []));
Спасибо за подробный ответ, но я думаю, что он не получает то, что я, я напишу весь свой код, чтобы было видно.В массиве 25 элементов в 5 группах.
var question = [
CP = [
{ questionNumber: "1", type: "CP", question: "paski?", answer1: "yo", answer2:"javier"},
{ questionNumber: "2", type: "CP", question: "pepino?", answer1: "si", answer2:"no"},
{ questionNumber: "3", type: "CP", question: "cabron?", answer1: "si", answer2:"a veces"},
{ questionNumber: "4", type: "CP", question: "ostia?", answer1: "carne", answer2:"pescado"},
{ questionNumber: "5", type: "CP", question: "adios.", answer1: "dormilon", answer2:"hablador"}
],
NP = [
{ questionNumber: "6", type: "NP", question: "quien es mas wapo?", answer1: "yo", answer2:"javier"},
{ questionNumber: "7", type: "NP", question: "te gusta viajar?", answer1: "si", answer2:"no"},
{ questionNumber: "8", type: "NP", question: "eres romantico?", answer1: "si", answer2:"a veces"},
{ questionNumber: "9", type: "NP", question: "que te gusta mas?", answer1: "carne", answer2:"pescado"},
{ questionNumber: "10", type: "NP", question: "eres mas...", answer1: "dormilon", answer2:"hablador"}
],
A = [
{ questionNumber: "11", type: "A", question: "paski?", answer1: "yo", answer2:"javier"},
{ questionNumber: "12", type: "A", question: "pepino?", answer1: "si", answer2:"no"},
{ questionNumber: "13", type: "A", question: "cabron?", answer1: "si", answer2:"a veces"},
{ questionNumber: "14", type: "A", question: "ostia?", answer1: "carne", answer2:"pescado"},
{ questionNumber: "15", type: "A", question: "adios.", answer1: "dormilon", answer2:"hablador"}
],
FC = [
{ questionNumber: "16", type: "FC", question: "paski?", answer1: "yo", answer2:"javier"},
{ questionNumber: "17", type: "FC", question: "pepino?", answer1: "si", answer2:"no"},
{ questionNumber: "18", type: "FC", question: "cabron?", answer1: "si", answer2:"a veces"},
{ questionNumber: "19", type: "FC", question: "ostia?", answer1: "carne", answer2:"pescado"},
{ questionNumber: "20", type: "FC", question: "adios.", answer1: "dormilon", answer2:"hablador"}
],
AC = [
{ questionNumber: "21", type: "AC", question: "paski?", answer1: "yo", answer2:"javier"},
{ questionNumber: "22", type: "AC", question: "pepino?", answer1: "si", answer2:"no"},
{ questionNumber: "23", type: "AC", question: "cabron?", answer1: "si", answer2:"a veces"},
{ questionNumber: "24", type: "AC", question: "ostia?", answer1: "carne", answer2:"pescado"},
{ questionNumber: "25", type: "AC", question: "adios.", answer1: "dormilon", answer2:"hablador"}
]
];
, так что здесь у меня есть 5-кратные функции для получения 2 случайных элементов из каждой группы.
var CPselected = [];
for (var i = 0; i < 2; i++){
rand();
}
function rand(){
var ran = CP[Math.floor(Math.random() * CP.length)];
if (CPselected.indexOf(ran) == -1)
CPselected.push(ran);
else
rand();
}
var NPselected = [];
for (var i = 0; i < 2; i++){
NPrand();
}
function NPrand(){
var ran = NP[Math.floor(Math.random() * NP.length)];
if (NPselected.indexOf(ran) == -1)
NPselected.push(ran);
else
NPrand();
}
var Aselected = [];
for (var i = 0; i < 2; i++){
Arand();
}
function Arand(){
var ran = A[Math.floor(Math.random() * A.length)];
if (Aselected.indexOf(ran) == -1)
Aselected.push(ran);
else
Arand();
}
var FCselected = [];
for (var i = 0; i < 2; i++){
FCrand();
}
function FCrand(){
var ran = FC[Math.floor(Math.random() * FC.length)];
if (FCselected.indexOf(ran) == -1)
FCselected.push(ran);
else
FCrand();
}
var ACselected = [];
for (var i = 0; i < 2; i++){
ACrand();
}
function ACrand(){
var ran = AC[Math.floor(Math.random() * AC.length)];
if (ACselected.indexOf(ran) == -1)
ACselected.push(ran);
else
ACrand();
}
Здесь я констатирую5 массивов в один
var Selected = CPselected.concat(NPselected, Aselected, FCselected, ACselected);
И здесь я показываю это в html
document.getElementById("data_area").innerHTML =
"<div id='question_1' class='question_data'><div class='no'>" +CPselected[0].questionNumber+ "</div><div class='type'>" +CPselected[0].type+ "</div><div class='question'>" + CPselected[0].question + "</div><div class='answer_1'>" + CPselected[0].answer1 + "</div><div class='answer_2'>" + CPselected[0].answer2 + "</div></div><div id='question_2' class='question_data'><div class='no'>" +CPselected[1].questionNumber+ "</div><div class='type'>" +CPselected[1].type+ "</div><div class='question'>" + CPselected[1].question + "</div><div class='answer_1'>" + CPselected[1].answer1 + "</div><div class='answer_2'>" + CPselected[1].answer2 + "</div></div>";