vikkktor

Arrays-Lab_Non-Decreasing Subsequence

Oct 15th, 2021 (edited)
81
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //-------------------------------------- Non-Decreasing Subsequence
  2. // Write a function that extracts only those numbers that form a non-decreasing subsequence.
  3. // In other words, you start from the first element and continue to the end of the given array of numbers.
  4. // Any number which is LESS THAN the current biggest one is ignored,
  5. //  alternatively if it’s equal or higher than the current biggest one you set it as the current
  6. //  biggest one and you continue to the next number.
  7.  
  8. function increasingSequence(array) {
  9.     let arrayOfNumbers = array;
  10.     let arrayOfNumbersL = arrayOfNumbers.length
  11.     let currentMax = arrayOfNumbers[0]
  12.     let newArray = [currentMax]
  13.     for (let i = 1; i < arrayOfNumbersL; i++) {
  14.         if (arrayOfNumbers[i] >= currentMax) {
  15.             currentMax = arrayOfNumbers[i]
  16.             newArray.push(currentMax)
  17.         }
  18.     }
  19.     console.log(newArray.join(' '))
  20.  
  21. }
RAW Paste Data