 + Нов въпрос EvgeniJD 22 Точки

## 4.Non-Decreasing Subsequence

Write a function that extracts only those numbers that form a non-decreasing subsequence. In other words, you start from the first element and continue to the end of the given array of numbers. Any number which is LESS THAN the current biggest one is ignored, alternatively if it’s equal or higher than the current biggest one you set it as the current biggest one and you continue to the next number.

### Input

The input comes as array of numbers.

### Output

The output is the processed array after the filtration, which should be a non-decreasing subsequence. The elements should be printed on one line, separated by a single space.

### Examples

 Input Output [ 1, 3, 8, 4, 10, 12, 3, 2, 24] 1 3 8 10 12 24 [ 1, 2, 3, 4] 1 2 3 4 [ 20, 3, 2, 15, 6, 1] 20

### Hints

• The Array.filter() built-in function might help you a lot with this problem.
Тагове:
0 willystyle 1779 Точки
``````function solve(arr) {
console.log(arr.filter((el, index) => el >= Math.max(...arr.slice(0, index))).join(' '));
}``````

или може би по-ефективно е:

``````function solve3(arr) {
let max = arr;
let result = arr.filter(el => {
if (el >= max) {
max = el;
}
return el >= max;
});
console.log(result.join(' '));
}``````

0
05/02/2020 11:33:44 EvgeniJD 22 Точки

Какво значат трите точки преди slice? На моето ниво второто решение е по-четимо :) Благодаря ти, отново!

1