2013-04-07 62 views
0

我有一個腳本需要捕獲一個值(textbox1),並使用該值來確定數組的大小。然後,我需要使用不同的文本框(tesxtbox2)來捕獲值來填充數組,然後處理一些計算。JavaScript:使用一個文本框作爲循環的基礎來捕獲第二個文本框中的值?

我可以捕獲第一值細,尺寸的陣列,且填充陣列,如果我使用的數據採集窗口提示。我無法弄清楚如何用文本框捕獲來替換提示方法。

這裏是代碼和註釋顯示我在哪裏卡住了。實質上,我需要用「通過id獲取元素」替換「window.prompt」部分並使用文本框,循環遍歷值直到輸入的初始數字(文本框1)被滿足。

  <html> 
      <head> 
       <meta charset = "utf-8"> 
       <title>Variance and Standard Deviation</title> 

      <style type = "text/css"> 

      #placement { 
      width: 50%; 
      padding: 0 0px; 
      align:right; 
      text-align: left; 
      } 

      #sorted { 
      width: 50%; 
      padding: 0 0px; 
      float:left; 
      text-align: left; 
      } 

      #mean,{ 
      width: 50%; 
      padding: 0 0px; 
      align:right; 
      text-align: left; 
      } 
      #variance,{ 
      width: 50%; 
      padding: 0 0px; 
      align:right; 
      text-align: left; 
      } 
      #deviation,{ 
      width: 50%; 
      padding: 0 0px; 
      position: relative; 
      text-align: left; 
      } 

      </style> 

      <script> 

      //declare and intialize variables 
      var capCount = 0; //capture count of variables to be entered 
      var count = 0; //parse captured count to an integer 
      var arrayTotal=0 ; //total of all values entered into array for math calcs 
      var arrayAverage=0; //average (mean) of values in array 
      var sortedArray = 0; //array sorted in ascending order 
      var values = 0; //values entered into text box for array population 


      function start(formInfo) 
       { 
        var countField = document.getElementById("count"); // gets entry from count field on form 
        var capCount = countField.value;  
        var count = parseInt (capCount); 

        var valueArray = new Array(); // allocate empty array 
        //start capture and populate array loop 
        for (var i = 0; i < count; ++i) //using loops count, prompts user to enter variables until count of loops (count) is reached 
        //STUCK HERE- replace variable capture from window prompt to get value from text box 
        // rather than using window prompt box, need to capture input from the "value" text box, 
        // loop through the text box each time a value is entered and process when the last value (loops equal to count) is entered 
         { 
         var variable = window.prompt("Please enter a value for value " + (i + 1) + ""); //prompts user to enter a value 
         //var variable = document.getElementById("value"); 
         valueArray [ i ] = variable ; 
         } //end capture for loop 


        //calls sort function to sort array into ascending order 
        valueArray.sort (sort); 

        //calls calcuate function to conduct math and get results 
        calculate (valueArray); 

       }// end build array function 



      //sort function 
      function sort (value1, value2) 
      { 
       return parseInt (value1) - parseInt(value2); 
      } //end sort 

      //calcuate function 
      function calculate (theArray) 
      { 
       //declare variables 
       n = 0; 
       arrayTotal = 0; 
       total_sqr = 0; 
       for (var i = 0; i < theArray.length; ++i) 
       { 
       arrayTotal += +theArray[i]; 
       n = (n + 1); 
       total_sqr += (+theArray[i] * +theArray[i]); 
       } 
       //mean, variance, deviation calcs 
       mean = (arrayTotal/theArray.length); //calcuate mean 
       variance = (total_sqr - ((arrayTotal*arrayTotal)/n))/(n - 1); 
       deviation = Math.sqrt(variance) ; 

       //calls output function to output the resuls of the script 
       output("The sorted order of the values entered is", theArray, document.getElementById("sorted")); 
       output("The mean of the values entered is", mean.toFixed(2), document.getElementById("mean"));  
       output("The variance of the values entered is", variance.toFixed(2), document.getElementById("variance")); 
       output("The standard deviation of the values entered is", deviation.toFixed(2), document.getElementById("deviation")); 

      } //end calculations 

      //output function 
      function output (heading, data, results) 
      { 

       var content = "<p>" + heading + "</p><p>" + data + "</p>" ; 
       results.innerHTML = content; // place the table in the output element 
      } //end output 

      </script> 
      </head> 
      <body> 

      <body> 
       <P><h2>This Page calcuates mean, variance and standard deviation</h2></p> 
       <form action = "#"> 
        <p><label><h3>Enter the # of values to process and click submit:</h3> 
        <input id = "count" type = "number" size = "10"></label> 

       <div> 
       <input type="button" value="Submit #of values to " onClick="start(this.form);"> 
       </div> 

       <p><label><h3>Enter the x value to process:</h3> 
       <input id = "value" type = "number" size = "10"></label> 

       <p> 
       <input type="button" value="process your values" onClick="process(this.form);"> 
       </p> 


       </form> 

      <div id = "sorted"></div> 
      <div id = "placement"> 
       <div id = "mean"></div> 
       <div id = "variance"></div> 
       <div id = "deviation"></div> 
      </div> 
      </body> 
     </html> 

回答

0

你應該點擊按鈕「處理你的價值觀」爲你做所有的工作。 論 「處理你的價值觀」,調用處理功能點擊,

//pseudo code 
function process(){ 
var length = document.getElementById('count').value 
var value_string = get_the_value_from_second_text_box 

convert value_string to an array(of values you need) 

do_the_computation 
display_the_results 

}

相關問題