JSFiddle - React, Tailwind, and code Playground
by Edi Carlos
HTML
<pre id="result"></pre>
<pre id="result2"></pre>
<pre id="result3"></pre>
JavaScript
myArray = [1,11,21,31,41,51,2,12,22,32,42,52,3,13,23,33,43,53,6,16,26,36,46,56,7,17,27,37,47,57,9,19,29,39,49,59];
myArray2 = [2,12,22,32,42,52,4,14,24,34,44,54,5,15,25,35,45,55,6,16,26,36,46,56,8,18,28,38,48,58,10,20,30,40,50,60];
myArray3 = [3,13,23,33,43,53,4,14,24,34,44,54,5,15,25,35,45,55,7,17,27,37,47,57,8,18,28,38,48,58,10,20,30,40,50,60];
myArrayConct3 = [2,12,22,32,42,52,4,14,24,34,44,54,6,16,26,36,46,56,1,11,21,31,41,51,9,19,29,39,49,59];
var rand = [];
var gen_nums = [];
var aux = [];
/*Verifica se ja saiu numero*/
function in_array(array, el) {
for(var i = 0 ; i < array.length; i++)
if(array[i] == el) return true;
return false;
}
/*Gera numeros não repetidos*/
function get_rand(array) {
var rand = array[Math.floor(Math.random()*array.length)];
if(!in_array(gen_nums, rand)) {
gen_nums.push(rand);
return rand;
}
return get_rand(array);
}
/*Gera 4 números*/
function getFourNumbers(array, repeat){
for(var i = 0; i < repeat; i++) {
rand.push(get_rand(array));
}
}
/*Gera 2 números*/
function getTwoNumbers(array, repeat){
for(var i = 0; i < repeat; i++) {
aux.push(get_rand(array));
}
}
/*Reseta vetores*/
function reset(){
aux = [];
rand = [];
}
/****************************/
/*Jogo 1*/
getFourNumbers(myArray, 4);
getTwoNumbers(myArray2, 2);
rand.push(aux);
document.getElementById('result').innerHTML = [...new Set(rand)];
reset();
/****************************/
/*Jogo 2*/
getFourNumbers(myArray2, 4);
getTwoNumbers(myArray, 2);
rand.push(aux);
document.getElementById('result2').innerHTML = [...new Set(rand)];
reset();
/****************************/
/*Jogo 3*/
getFourNumbers(myArray3, 4);
getTwoNumbers(myArrayConct3, 2);
rand.push(aux);
document.getElementById('result3').innerHTML = [...new Set(rand)];
reset();