Sea Sun

Sea Sun

  • NA
  • 10
  • 4.1k

How to write arrays & loops to display the span of stocks

Mar 27 2015 12:00 PM

For my assignment, I need 5 text boxes for entering 5 stock prices (which I have)
I added a "SHow Span" button but I do not know how to write array and loops to display in the last textbox to display the span (like[1, 1, 2, 1, 4, 5,]).
I was given some hints with the codes but I do not know how to work them.  

I have this so far

<script>
 //Array declarations
 
 var numbers = [100, 60, 70, 65, 80, 85];
 var servings
 var arr = new Array();
 //var arr2 = new Array (5);
 arr[0] = 1;
 arr[1] = 2;
 arr[2] = 3;
 arr[3] = 4;
 arr[4] = 5;
 
 var value = arr[2];
 var prices= new Array ("")

  var
  S[0] = 1; // because the span for the first day is 1.
  
  for ( i = 1; i < 5; i++)
  {
 
  S[i] = 1; // Initialize span value
  
  // Iterate while the next item on left is smaller than price [i]
  for ( j = i-1; (j>=0) && (price[i]>=price[j]); j--)
   {
    S[i]++;
   }
  }
  
  //Loop that writes the calculated span to a string.
  var span = ""; // Empty string to start
  
  forr (var k=0; k<5; k++)
  {
   span = span = S[i] + " ";
  }
  // Return the string
  return span;
  }
  
</script>


Answers (1)