04. Non-Decreasing Subsequence / JavaScript
Здравейте, някой може ли да ми покаже как да реша задачата с .filter();
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.
Какво значат трите точки преди slice? На моето ниво второто решение е по-четимо :) Благодаря ти, отново!
това е spread operator, превръща масив в iterable: https://developer.mozilla.org/bg/docs/Web/JavaScript/Reference/Operators/%D1%80%D0%B0%D0%B7%D0%BF%D1%80%D0%B5%D0%B4%D0%B5%D0%BB%D1%8F%D1%89_%D1%81%D0%B8%D0%BD%D1%82%D0%B0%D0%BA%D1%81%D0%B8%D1%81