Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-------------------------------------- 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.
- function increasingSequence(array) {
- let arrayOfNumbers = array;
- let arrayOfNumbersL = arrayOfNumbers.length
- let currentMax = arrayOfNumbers[0]
- let newArray = [currentMax]
- for (let i = 1; i < arrayOfNumbersL; i++) {
- if (arrayOfNumbers[i] >= currentMax) {
- currentMax = arrayOfNumbers[i]
- newArray.push(currentMax)
- }
- }
- console.log(newArray.join(' '))
- }
Add Comment
Please, Sign In to add comment