Advertisement
Guest User

Problem 7. Maximal Increasing Sequence

a guest
Jul 21st, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Write a JavaScript function findMaxSequence(value) that finds the maximal increasing sequence in an array of numbers and returns the result as an array. If there is no increasing sequence the function returns 'no'.
  2. // Write JS program maxSequenceFinder.js that invokes your function with the sample input data below and prints the output at the console. Examples:
  3. // Input    Output
  4. // [3, 2, 3, 4, 2, 2, 4]        [2, 3, 4]
  5. // [3, 5, 4, 6, 1, 2, 3, 6, 10, 32] [1, 2, 3, 6, 10, 32]
  6. // [3, 2, 1]    no
  7.  
  8.  
  9. function findMaxSequence (argument) {
  10.     argument.push(1);
  11.     var tempArray = [];
  12.     var resultArray = [];
  13.     for (var i = 0; i < argument.length; i++) {        
  14.         if(argument[i] > argument[i-1]){   
  15.             tempArray.push(argument[i]);
  16.            
  17.         } else{
  18.             tempArray= [];
  19.             continue;
  20.         }
  21.         if (argument[i] < argument[i-1] && argument[i] < argument[i+1]) {
  22.                    
  23.                     tempArray.push(argument[i]);
  24.         }
  25.         if (tempArray.length >= resultArray.length) {
  26.             resultArray= tempArray;
  27.         } else {
  28.             tempArray=[];
  29.         }
  30.  
  31.     }
  32.     console.log(tempArray);
  33.     console.log(resultArray);
  34. }
  35. findMaxSequence([3, 2, 3, 4, 2, 2, 4]);
  36. findMaxSequence([3, 5, 4, 6, 1, 2, 3, 6, 10, 32]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement