The task is as follows:
var streak = { min: '', max: '', length: '', series: [] } var allStreaks = [] I have an array of numbers:
var arr = [19, 20, 21, 22, 17, 18, 19, 7, 8, 9] I need to distribute them across arrays so that these numbers are in it in ascending order, and write it all in this format:
[{ min: 19, max: 22, length: 4, series: [19, 20, 21, 22] }, { min: 17, max: 19, length: 3, series: [17, 18, 19] }, { min: 7, max: 9, length: 3, series: [7, 8, 9] }] In the original array, the numbers go in the order in which I need them (as if these numbers went on dates - one number - one day) I need to catch how many "days" these numbers increased, from what date it started and on which it stopped and then all over again. The first 4 numbers of the arr array (19 20 21 22) should create an object in which the minimum number (19) will be written, the maximum number (22), how many total numbers were written (4) and the array of these numbers itself [19, 20, 21 , 22]. Likewise, with the second incremental streak from 17 to 19, etc. The numbers in the source array are absolutely random and you need to track this sequence of increases and as soon as you find a number less than the previous one, start a new array.
Currently doing this:
for (var i = 0; i < arr.length-1; i++) { if (arr[i] < arr[i+1]) { streak.series.push(arr[i]) } else { streak.series.push(arr[i]) //Сохранит последний элемент первого стрика break; } } So I successfully get the first streak, but if I need to continue, then difficulties arise:
for (var i = 0; i < arr.length-1; i++) { if (arr[i] < arr[i+1]) { streak.series.push(arr[i]) } else if ((arr[i] > arr[i-1]) && (arr[i] > arr[i+1])) { streak.series.push(arr[i]) // Это ловит и записывает последний элемент первого стрика, получается steak.series = [19,20,21,22] // но он на этом не останавливается и продолжает записывать все числа исходного массива // так как они потом подходят под первый if (arr[i] < arr[i+1]) // и в результате у меня просто переписывается весь исходный массив } else {continue;} }