let arr1 = [ { value: '2'}, { value: '4'}, { value: '11'}, { value: '7'}, { value: '8'}, { value: '9'} ];
let arr2 = [ { value: '5'}, { value: '20'}, { value: '5'}, { value: '7'}, { value: '9'}, { value: '11'} ];
function findSequence(arr) {
const fistSequenceEl = arr.findLastIndex((e, i) => i === 0 || +e.value < +arr[i - 1].value);
return arr.slice(fistSequenceEl);
}
function findSequence(arr) {
let maxSequence = [];
let currentSequence = [];
for (let i = 1; i < arr.length - 1; i++) {
const currentValue = parseInt(arr[i].value, 10);
const prevValue = parseInt(arr[i - 1].value, 10);
const nextValue = parseInt(arr[i + 1].value, 10);
if (prevValue > currentValue && currentValue < nextValue) {
currentSequence = [arr[i]];
} else if (currentSequence.length > 0 && currentValue < nextValue) {
currentSequence.push(arr[i]);
} else {
currentSequence = [];
}
if (currentSequence.length > maxSequence.length) {
maxSequence = currentSequence.slice();
}
}
if (currentSequence.length > 0 && parseInt(arr[arr.length - 1].value, 10) > parseInt(arr[arr.length - 2].value, 10)) {
currentSequence.push(arr[arr.length - 1]);
if (currentSequence.length > maxSequence.length) {
maxSequence = currentSequence.slice();
}
}
return maxSequence;
}
let arr1 = [{ value: '2' }, { value: '4' }, { value: '11' }, { value: '7' }, { value: '8' }, { value: '9' }];
let arr2 = [{ value: '5' }, { value: '20' }, { value: '5' }, { value: '7' }, { value: '9' }, { value: '11' }];
console.log(findSequence(arr1)); // (7, 8, 9)
console.log(findSequence(arr2)); // (5, 7, 9, 11)