The meaning of the game is 20 rounds; can fall out one of 3 values ​​(randomly); in the end, I need to know how many times the combination of the maximum number of zeros fell out, i.e., for example, here

1,2,2,1,1,1,1,1,0,0,0,0,2,1,1,2,0,0,2,1 

The result should be 4, respectively, since this is the maximum number of zeroes in a row.

Can you please tell me how to do this?

 var stavka = []; //Может быть 0, 1, 2 как ниже в переменных var red = 0; var black = 1; var zero = 2; alert(stavka); var raunds = 20; //количество бросков var i = 0; // счетчик for(i=0; i<raunds; i++) { stavka.push(Math.round(Math.random()*2)); //происходит случайный выбор числа (0, 1, 2) в 20 бросков } var rezultat=(stavka); //Записываю массив в переменную for (i=0; i<rezultat.length; i++) { //Получаю кол-во сколько раз выпало Красное (var red = 0;) if (rezultat[i] == 0 ) { red++; } } for(find in rezultat){ //Получаю индексы всех значений выпавших Красное (var red = 0;) if(rezultat[find]=="0") { var index=(rezultat); alert(find) } } alert("Количество выпадения red" + red); //Как связать поиск нулей, их индексов и получить результат в виде количества нулей в максимальной последовательности? 
  • one
    Is this how it was intended to make the value "1" twice as often as "0" and "2"? - Yaant

2 answers 2

An example for 2 values ​​- red and white:

 // красное - черное - зеро вероятность выпадения var arr = []; var rounds = 100; var n = 0; var i = 0; var max_length = 0; var length = 0; for (i = 0; i <= rounds; i++) { n = Math.round(Math.random() * 36); if ( n != 0) { n = (n % 2) * 2 - 1; } arr.push(n); } console.log(arr); for (i = 0; i < arr.length; i++) { if (arr[i] == 1) { length = 0; while (i < arr.length && arr[i] == 1) { i++; length++; } if (length > max_length) { max_length = length; } } } console.log('Максимально длинная красная последовательность: ' + max_length); 

     let s = '112220000011100', r = {}; s.replace(/(\d)\1*/g, _ => r[_[0]] = Math.max(r[_[0]] || 0, _.length)); console.info(r['0']); // 5 

    • what magic is going on here? Please explain: s.replace (/ (\ d) \ 1 * / g, _ => r [_ [0]] = Math.max (r [_ [0]] || 0, _.length)); - spectre_it
    • @ stas0k, we look for any maximal sequences that are the same digits and add the maximum length by key-digit — either what is, or new (if it is longer). At the output, we obtain an object with the keys from the present numbers (unique) and the length value of the maximum sequence of this digit. - user207618
    • How did you learn this? After all, it is not at all obvious that here we are looking for the maximum sequence s.replace (/ (\ d) \ 1 * / g, _. The underscore mark is still. - spectre_it
    • one
      @ stas0k, learning and experience, my friend :) Regular expression says: take a digit and, possibly, its maximum repetition. An underscore is just a variable. - user207618