Advertisement
dimipan80

Maximal Increasing Sequence

Nov 12th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Write a JavaScript function findMaxSequence(arr) that finds the maximal increasing sequence
  2. in an array of numbers and returns the result as an array. If there is no increasing sequence
  3. the function returns 'no'. Write JS program maxSequenceFinder.js that invokes your function
  4. with the sample input data below and prints the output at the console. */
  5.  
  6.  "use strict";
  7.  
  8. function findMaxSequence(arr) {
  9.     if (arr.length <= 1) {
  10.         return 'no';
  11.     } else {
  12.         var resultArr = [];
  13.         var tempArr = [arr[0]];
  14.         var i;
  15.         for (i = 1; i < arr.length; i += 1) {
  16.             if (arr[i] > arr[i - 1]) {
  17.                 tempArr.push(arr[i]);
  18.             } else {
  19.                 if (tempArr.length > resultArr.length) {
  20.                     resultArr = tempArr.slice(0);
  21.                 }
  22.                 tempArr = [arr[i]];
  23.             }
  24.         }
  25.  
  26.         if (tempArr.length > resultArr.length) {
  27.             resultArr = tempArr.slice(0);
  28.         }
  29.  
  30.         if (resultArr.length > 1) return resultArr;
  31.         else return 'no';
  32.     }
  33. }
  34.  
  35. console.log(findMaxSequence([3, 2, 3, 4, 2, 2, 4]));
  36. console.log(findMaxSequence([3, 5, 4, 6, 1, 2, 3, 6, 10, 32]));
  37. console.log(findMaxSequence([3, 2, 1]));
  38. console.log(findMaxSequence([4, 4, 5, 6, 10, 32, 1, 2, 3, 4, 5, 6]));
  39. console.log(findMaxSequence([5]));
  40. console.log(findMaxSequence([-9.3, -6.5, -1.5, 7.8, 3.2, 3.5, 4.3, 5.4, 6.2, 2.1, 4.8]));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement