vonko1988

JSSequenceEqualElements

May 8th, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function execute() {
  2.     var output = ' ';
  3.     var i;
  4.     var n = prompt("Enter the length of the first array: ");
  5.     var array = new Array(n);
  6.     for (i = 0; i < n; i++) {
  7.         array[i] = prompt("Enter element with index: " + i);
  8.     }
  9.  
  10.     var currentElement = array[0];
  11.     var currentSequenceLength = 1;
  12.     var maxElement = array[0];
  13.     var maxSequenceLength = 1;
  14.     var tempStartIndex = 0;
  15.     var startIndex = 0;
  16.     var endIndex = 0;
  17.  
  18.     var equalsArray = new Array;
  19.  
  20.     for (var i = 1; i < array.length; i++) {
  21.  
  22.         if (array[i] != currentElement) {
  23.             currentElement = array[i];      
  24.             currentSequenceLength = 1;
  25.             tempStartIndex = i;
  26.         }
  27.         else if (array[i] == currentElement) {
  28.             currentSequenceLength++;
  29.         }
  30.  
  31.         if (currentSequenceLength > maxSequenceLength) {
  32.             maxElement = currentElement;
  33.             equalsArray.pop();
  34.             equalsArray.pop();
  35.             equalsArray.push(maxElement);
  36.             maxSequenceLength = currentSequenceLength;
  37.             startIndex = tempStartIndex;
  38.             endIndex = i;
  39.         }
  40.         if (currentSequenceLength == maxSequenceLength &&
  41.             currentElement != maxElement) {
  42.             equalsArray.push(currentElement);
  43.         }
  44.     }
  45.  
  46.  
  47.     for (i = 0; i < equalsArray.length; i++) {
  48.  
  49.         for (j = 0; j < maxSequenceLength; j++) {
  50.             output += equalsArray[i] + ' ';
  51.         }
  52.  
  53.         output += '<br/>'
  54.     }
  55.  
  56.  
  57.     document.getElementById('output').innerHTML = output;
  58. }
  59.  
  60.  
  61. function reset() {
  62.     location.reload();
  63. }
  64.  
  65. document.getElementById('execute').onclick = execute;
  66. document.getElementById('reset').onclick = reset;;
Advertisement
Add Comment
Please, Sign In to add comment