task We enter the n-th number of array elements. We derive the initial array and the maximum series of successive positive elements (ascending with step +1) [1,3,6,5,6,7,8,4,1,4,5] - > maximum series [5,6,7,8]

that's what happens with me

var mas=[]; do { mas.push(+prompt('Π’Π²Π΅Π΄ΠΈΡ‚Π΅ число')) } while(mas[mas.length-1]!=0) mas.pop(); console.log(mas.join(" ")); var max=mas[0]; for (var i=0;i < mas.length;i++){ if(mas[i]>max) max=mas[i]; } console.log("ΠœΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹Π΅ числа Π² массивС =",max); 

I can not figure out how to find out the series and select positive numbers from it ...

    1 answer 1

     document.getElementById('run').onclick = () => { var mas = document.getElementById('input').value.split(',').map(i => +i); let max=0, max_series=[]; mas.reduce((result,item) => { //Π‘Π½Π°Ρ‡Π°Π»Π° Ρ€Π°Π·Π±ΠΈΠ²Π°Π΅ΠΌ массив Π½Π° сСрии let series = result[result.length-1]; if(series && item === (1 + series[series.length-1])){ series.push(item); }else{ result.push([item]); } return result; },[]).reduce((max_length,series)=>{ //ΠΏΠΎΡ‚ΠΎΠΌ ΠΈΡ‰Π΅ΠΌ ΡΠ°ΠΌΡƒΡŽ Π΄Π»ΠΈΠ½Π½ΡƒΡŽ if(max<series.length){ //здСсь ΠΌΠΎΠΆΠ½ΠΎ ΠΈΠ·ΠΌΠ΅Π½ΠΈΡ‚ΡŒ условиС, //Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€, Π½Π°ΠΉΡ‚ΠΈ ΡΠ΅Ρ€ΠΈΡŽ с самой большой суммой чисСл max = series.length; max_series= series; } return max; }, max); console.log("Бамая длинная сСрия - ", max_series); } 
     <textarea id='input'> 0,6,2,1,4,5,6,7,1,3,4,1,2,3,4,5,6,7,6,5,2,7,8,21,4,8,9,4,1,3,4 </textarea> <button id='run'>Run</button> 

    Check that in the series only positive numbers are on your conscience)

    • thanks =) I will understand) - Arthur