2) Loop through the 8 "ttt" values in the shuffled array and add the first object found to a new array "questionObjects"
5) Find the first object in this newly filtered array and add it to "questionObjects"
6) Remove all objects with the same "ttt" value as above from the shuffled array
7) Again, find the first object in this newly filtered array and add it to "questionObjects"
Load the question input with the "id" values from "questionObjects" and with the "id" values from the corresponding (from "relID") objects in the B array
Code:
<script type="text/javascript" data-author="Tony Partner">
$(document).on('ready pjax:scriptcomplete',function(){
var questionsArrayA = [
{ "id":"001","ttt":"7","xn":"F4","relID":"1" },
{ "id":"002","ttt":"7","xn":"F4","relID":"2" },
{ "id":"003","ttt":"7","xn":"E3","relID":"3" },
{ "id":"004","ttt":"7","xn":"E3","relID":"4" },
{ "id":"005","ttt":"7","xn":"F4","relID":"5" },
{ "id":"006","ttt":"7","xn":"F4","relID":"6" },
{ "id":"007","ttt":"7","xn":"E3","relID":"7" },
{ "id":"008","ttt":"7","xn":"E3","relID":"8" },
{ "id":"009","ttt":"7","xn":"D2","relID":"9" },
{ "id":"010","ttt":"7","xn":"D2","relID":"10" },
{ "id":"011","ttt":"8","xn":"D1","relID":"11" },
{ "id":"012","ttt":"8","xn":"D1","relID":"12" },
{ "id":"013","ttt":"7","xn":"D2","relID":"13" },
{ "id":"014","ttt":"7","xn":"D2","relID":"14" },
{ "id":"015","ttt":"8","xn":"D1","relID":"15" },
{ "id":"016","ttt":"8","xn":"D1","relID":"16" },
{ "id":"017","ttt":"1","xn":"C6","relID":"17" },
{ "id":"018","ttt":"1","xn":"C6","relID":"18" },
{ "id":"019","ttt":"2","xn":"B5","relID":"19" },
{ "id":"020","ttt":"2","xn":"B5","relID":"20" },
{ "id":"021","ttt":"1","xn":"C6","relID":"21" },
{ "id":"022","ttt":"1","xn":"C6","relID":"22" },
{ "id":"023","ttt":"2","xn":"B5","relID":"23" },
{ "id":"024","ttt":"2","xn":"B5","relID":"24" },
{ "id":"025","ttt":"5","xn":"A4","relID":"25" },
{ "id":"026","ttt":"5","xn":"A3","relID":"26" },
{ "id":"027","ttt":"5","xn":"A4","relID":"27" },
{ "id":"028","ttt":"5","xn":"A3","relID":"28" },
{ "id":"029","ttt":"5","xn":"C2","relID":"29" },
{ "id":"030","ttt":"6","xn":"B1","relID":"30" },
{ "id":"031","ttt":"5","xn":"C2","relID":"31" },
{ "id":"032","ttt":"6","xn":"B1","relID":"32" },
{ "id":"033","ttt":"1","xn":"A6","relID":"33" },
{ "id":"034","ttt":"1","xn":"A6","relID":"34" },
{ "id":"035","ttt":"2","xn":"A5","relID":"35" },
{ "id":"036","ttt":"2","xn":"A5","relID":"36" },
{ "id":"037","ttt":"5","xn":"A4","relID":"37" },
{ "id":"038","ttt":"5","xn":"A3","relID":"38" },
{ "id":"039","ttt":"5","xn":"A4","relID":"39" },
{ "id":"040","ttt":"5","xn":"A3","relID":"40" },
{ "id":"041","ttt":"1","xn":"A6","relID":"41" },
{ "id":"042","ttt":"1","xn":"A6","relID":"42" },
{ "id":"043","ttt":"2","xn":"A5","relID":"43" },
{ "id":"044","ttt":"2","xn":"A5","relID":"44" },
{ "id":"045","ttt":"5","xn":"C2","relID":"45" },
{ "id":"046","ttt":"6","xn":"B1","relID":"46" },
{ "id":"047","ttt":"5","xn":"C2","relID":"47" },
{ "id":"048","ttt":"6","xn":"B1","relID":"48" },
{ "id":"049","ttt":"1","xn":"C6","relID":"49" },
{ "id":"050","ttt":"1","xn":"C6","relID":"50" },
{ "id":"051","ttt":"2","xn":"B5","relID":"51" },
{ "id":"052","ttt":"2","xn":"B5","relID":"52" },
{ "id":"053","ttt":"1","xn":"C6","relID":"53" },
{ "id":"054","ttt":"1","xn":"C6","relID":"54" },
{ "id":"055","ttt":"2","xn":"B5","relID":"55" },
{ "id":"056","ttt":"2","xn":"B5","relID":"56" },
{ "id":"057","ttt":"1","xn":"A8","relID":"57" },
{ "id":"058","ttt":"1","xn":"A8","relID":"58" },
{ "id":"059","ttt":"2","xn":"A7","relID":"59" },
{ "id":"060","ttt":"2","xn":"A7","relID":"60" },
{ "id":"061","ttt":"1","xn":"A8","relID":"61" },
{ "id":"062","ttt":"1","xn":"A8","relID":"62" },
{ "id":"063","ttt":"2","xn":"A7","relID":"63" },
{ "id":"064","ttt":"2","xn":"A7","relID":"64" },
{ "id":"065","ttt":"7","xn":"F4","relID":"65" },
{ "id":"066","ttt":"7","xn":"F4","relID":"66" },
{ "id":"067","ttt":"7","xn":"E3","relID":"67" },
{ "id":"068","ttt":"7","xn":"E3","relID":"68" },
{ "id":"069","ttt":"7","xn":"F4","relID":"69" },
{ "id":"070","ttt":"7","xn":"F4","relID":"70" },
{ "id":"071","ttt":"7","xn":"E3","relID":"71" },
{ "id":"072","ttt":"7","xn":"E3","relID":"72" },
{ "id":"073","ttt":"3","xn":"D6","relID":"73" },
{ "id":"074","ttt":"4","xn":"D5","relID":"74" },
{ "id":"075","ttt":"3","xn":"D6","relID":"75" },
{ "id":"076","ttt":"4","xn":"D5","relID":"76" },
{ "id":"077","ttt":"3","xn":"F8","relID":"77" },
{ "id":"078","ttt":"4","xn":"E7","relID":"78" },
{ "id":"079","ttt":"3","xn":"F8","relID":"79" },
{ "id":"080","ttt":"4","xn":"E7","relID":"80" },
{ "id":"081","ttt":"7","xn":"D4","relID":"81" },
{ "id":"082","ttt":"7","xn":"D4","relID":"82" },
{ "id":"083","ttt":"7","xn":"D3","relID":"83" },
{ "id":"084","ttt":"7","xn":"D3","relID":"84" },
{ "id":"085","ttt":"3","xn":"D6","relID":"85" },
{ "id":"086","ttt":"4","xn":"D5","relID":"86" },
{ "id":"087","ttt":"3","xn":"D6","relID":"87" },
{ "id":"088","ttt":"4","xn":"D5","relID":"88" },
{ "id":"089","ttt":"7","xn":"D4","relID":"89" },
{ "id":"090","ttt":"7","xn":"D4","relID":"90" },
{ "id":"091","ttt":"7","xn":"D3","relID":"91" },
{ "id":"092","ttt":"7","xn":"D3","relID":"92" },
{ "id":"093","ttt":"3","xn":"F8","relID":"93" },
{ "id":"094","ttt":"4","xn":"E7","relID":"94" },
{ "id":"095","ttt":"3","xn":"F8","relID":"95" },
{ "id":"096","ttt":"4","xn":"E7","relID":"96" }
];
var questionsArrayB = [
{ "id":"097","ttt":"7","xn":"F4","relID":"1" },
{ "id":"098","ttt":"7","xn":"F4","relID":"2" },
{ "id":"099","ttt":"7","xn":"E3","relID":"3" },
{ "id":"100","ttt":"7","xn":"E3","relID":"4" },
{ "id":"101","ttt":"7","xn":"F4","relID":"5" },
{ "id":"102","ttt":"7","xn":"F4","relID":"6" },
{ "id":"103","ttt":"7","xn":"E3","relID":"7" },
{ "id":"104","ttt":"7","xn":"E3","relID":"8" },
{ "id":"105","ttt":"7","xn":"D2","relID":"9" },
{ "id":"106","ttt":"7","xn":"D2","relID":"10" },
{ "id":"107","ttt":"8","xn":"D1","relID":"11" },
{ "id":"108","ttt":"8","xn":"D1","relID":"12" },
{ "id":"109","ttt":"7","xn":"D2","relID":"13" },
{ "id":"110","ttt":"7","xn":"D2","relID":"14" },
{ "id":"111","ttt":"8","xn":"D1","relID":"15" },
{ "id":"112","ttt":"8","xn":"D1","relID":"16" },
{ "id":"113","ttt":"1","xn":"C6","relID":"17" },
{ "id":"114","ttt":"1","xn":"C6","relID":"18" },
{ "id":"115","ttt":"2","xn":"B5","relID":"19" },
{ "id":"116","ttt":"2","xn":"B5","relID":"20" },
{ "id":"117","ttt":"1","xn":"C6","relID":"21" },
{ "id":"118","ttt":"1","xn":"C6","relID":"22" },
{ "id":"119","ttt":"2","xn":"B5","relID":"23" },
{ "id":"120","ttt":"2","xn":"B5","relID":"24" },
{ "id":"121","ttt":"5","xn":"A4","relID":"25" },
{ "id":"122","ttt":"5","xn":"A3","relID":"26" },
{ "id":"123","ttt":"5","xn":"A4","relID":"27" },
{ "id":"124","ttt":"5","xn":"A3","relID":"28" },
{ "id":"125","ttt":"5","xn":"C2","relID":"29" },
{ "id":"126","ttt":"6","xn":"B1","relID":"30" },
{ "id":"127","ttt":"5","xn":"C2","relID":"31" },
{ "id":"128","ttt":"6","xn":"B1","relID":"32" },
{ "id":"129","ttt":"1","xn":"A6","relID":"33" },
{ "id":"130","ttt":"1","xn":"A6","relID":"34" },
{ "id":"131","ttt":"2","xn":"A5","relID":"35" },
{ "id":"132","ttt":"2","xn":"A5","relID":"36" },
{ "id":"133","ttt":"5","xn":"A4","relID":"37" },
{ "id":"134","ttt":"5","xn":"A3","relID":"38" },
{ "id":"135","ttt":"5","xn":"A4","relID":"39" },
{ "id":"136","ttt":"5","xn":"A3","relID":"40" },
{ "id":"137","ttt":"1","xn":"A6","relID":"41" },
{ "id":"138","ttt":"1","xn":"A6","relID":"42" },
{ "id":"139","ttt":"2","xn":"A5","relID":"43" },
{ "id":"140","ttt":"2","xn":"A5","relID":"44" },
{ "id":"141","ttt":"5","xn":"C2","relID":"45" },
{ "id":"142","ttt":"6","xn":"B1","relID":"46" },
{ "id":"143","ttt":"5","xn":"C2","relID":"47" },
{ "id":"144","ttt":"6","xn":"B1","relID":"48" },
{ "id":"145","ttt":"1","xn":"C6","relID":"49" },
{ "id":"146","ttt":"1","xn":"C6","relID":"50" },
{ "id":"147","ttt":"2","xn":"B5","relID":"51" },
{ "id":"148","ttt":"2","xn":"B5","relID":"52" },
{ "id":"149","ttt":"1","xn":"C6","relID":"53" },
{ "id":"150","ttt":"1","xn":"C6","relID":"54" },
{ "id":"151","ttt":"2","xn":"B5","relID":"55" },
{ "id":"152","ttt":"2","xn":"B5","relID":"56" },
{ "id":"153","ttt":"1","xn":"A8","relID":"57" },
{ "id":"154","ttt":"1","xn":"A8","relID":"58" },
{ "id":"155","ttt":"2","xn":"A7","relID":"59" },
{ "id":"156","ttt":"2","xn":"A7","relID":"60" },
{ "id":"157","ttt":"1","xn":"A8","relID":"61" },
{ "id":"158","ttt":"1","xn":"A8","relID":"62" },
{ "id":"159","ttt":"2","xn":"A7","relID":"63" },
{ "id":"160","ttt":"2","xn":"A7","relID":"64" },
{ "id":"161","ttt":"7","xn":"F4","relID":"65" },
{ "id":"162","ttt":"7","xn":"F4","relID":"66" },
{ "id":"163","ttt":"7","xn":"E3","relID":"67" },
{ "id":"164","ttt":"7","xn":"E3","relID":"68" },
{ "id":"165","ttt":"7","xn":"F4","relID":"69" },
{ "id":"166","ttt":"7","xn":"F4","relID":"70" },
{ "id":"167","ttt":"7","xn":"E3","relID":"71" },
{ "id":"168","ttt":"7","xn":"E3","relID":"72" },
{ "id":"169","ttt":"3","xn":"D6","relID":"73" },
{ "id":"170","ttt":"4","xn":"D5","relID":"74" },
{ "id":"171","ttt":"3","xn":"D6","relID":"75" },
{ "id":"172","ttt":"4","xn":"D5","relID":"76" },
{ "id":"173","ttt":"3","xn":"F8","relID":"77" },
{ "id":"174","ttt":"4","xn":"E7","relID":"78" },
{ "id":"175","ttt":"3","xn":"F8","relID":"79" },
{ "id":"176","ttt":"4","xn":"E7","relID":"80" },
{ "id":"177","ttt":"7","xn":"D4","relID":"81" },
{ "id":"178","ttt":"7","xn":"D4","relID":"82" },
{ "id":"179","ttt":"7","xn":"D3","relID":"83" },
{ "id":"180","ttt":"7","xn":"D3","relID":"84" },
{ "id":"181","ttt":"3","xn":"D6","relID":"85" },
{ "id":"182","ttt":"4","xn":"D5","relID":"86" },
{ "id":"183","ttt":"3","xn":"D6","relID":"87" },
{ "id":"184","ttt":"4","xn":"D5","relID":"88" },
{ "id":"185","ttt":"7","xn":"D4","relID":"89" },
{ "id":"186","ttt":"7","xn":"D4","relID":"90" },
{ "id":"187","ttt":"7","xn":"D3","relID":"91" },
{ "id":"188","ttt":"7","xn":"D3","relID":"92" },
{ "id":"189","ttt":"3","xn":"F8","relID":"93" },
{ "id":"190","ttt":"4","xn":"E7","relID":"94" },
{ "id":"191","ttt":"3","xn":"F8","relID":"95" },
{ "id":"192","ttt":"4","xn":"E7","relID":"96" }
];
// Shuffle questionsArrayA
var shuffledA = shuffleArray(questionsArrayA);
var questionObjects = [];
var questionXns = [];
var questionIDs = [];
var questionRelIDs = [];
//Loop through the 8 "ttt" values in the shuffled array
for(var i=1; i <= 8; i++){
var tttValue = i;
// Find the first object in the questionObjects array with this "ttt"
var filteredItems = shuffledA.filter(function(obj) {
return (obj.ttt == tttValue);
});
var item1 = filteredItems[0];
// Add this first object to the questionObjects array
questionObjects.push(item1);
// Add this first object "xn" value to the questionXns array
questionXns.push(item1.xn);
// Add this first object "id" value to the questionIDs array
questionIDs.push(item1.id);
questionRelIDs.push(item1.relID);
// Remove this first object from the shuffled array
var shuffledA = shuffledA.filter(function(obj) {
return (obj.id !== item1.id);
});
}
// Remove all of the previously selected "xn" values from the shuffled array
var shuffledA2 = shuffledA;
$.each(questionXns, function(i, val) {
shuffledA2 = shuffledA2.filter(function(obj) {
return (obj.xn !== val);
});
});
// Find the first object in this newly filtered array
var filteredItem1 = shuffledA2[0];
var filteredItem1Ttt = filteredItem1.ttt;
// Add the first object in the newly filtered shuffled array to the questionObjects array
questionObjects.push(filteredItem1);
// Add this first object "id" value to the questionIDs array
questionIDs.push(filteredItem1.id);
questionRelIDs.push(filteredItem1.relID);
// Now remove all objects with the same "ttt" value from the shuffled array
shuffledA2 = shuffledA2.filter(function(obj) {
return (obj.ttt !== filteredItem1Ttt);
});
// Again, find the first object in this newly filtered array
var filteredItem2 = shuffledA2[0];
// Again, add the first object in the newly filtered shuffled array to the questionObjects array
questionObjects.push(filteredItem2);
// Add this first object "id" value to the questionIDs array
questionIDs.push(filteredItem2.id);
questionRelIDs.push(filteredItem2.relID);
// Add the associated "id" values from the questionsArrayB array to the questionIDs array
$.each(questionRelIDs, function(i, val) {
var bObject = questionsArrayB.filter(function(obj) {
return (obj. "questionObjects" == val);
})[0];
questionIDs.push(bObject.id);
});
$('#question{QID} input[type="text"]').val('#,'+questionIDs).trigger('keyup');
//$('#question{QID}').hide();
//$('#ls-button-submit').trigger('click');
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
});
</script>