2017-03-15 206 views
1

這可能是一個愚蠢的問題,但我對JavaScript很陌生,不能在我的生活中找出如何獲得用戶輸入數組元素的平均值(平均值)。這裏是我編寫的代碼,其中提示用戶輸入數組中的元素數量並循環回來並多次提示輸入元素。任何幫助表示讚賞。謝謝。如何獲取用戶輸入數組元素的平均值(平均值)?

<html> 
<head> 
<title>Title</title> 
<script type="text/javascript"> 
    var nums = new Array(); 
    var N = prompt("How many numbers would you like to enter? "); 
    N = parseInt(N); 
    var i = 0; 
    for(i = 0; i <= N - 1; i++) 
    { 
     nums[i] = prompt("Enter your numbers: "); 
     document.write("Number you chose: " + nums[i] + "<br />"); 
    } 
</script> 
</head> 
<body> 
</body> 
</html> 
+0

'變種總和= 0;'環路,'總和+ =號碼(NUMS [I])'循環內(後與提示線)和'VAR平均=總和/ N之前;循環之後。 – nnnnnn

回答

1

你必須使用一個總和VAR s增加的輸入值,並使用這個公式average = s/N計算在年底平均:

<script type="text/javascript"> 
 
    var nums = new Array(); 
 
    var N = prompt("How many numbers would you like to enter? "); 
 
    N = parseInt(N); 
 
    var i = 0, s = 0; 
 
    for(i = 0; i <= N - 1; i++) 
 
    { 
 
     nums[i] = parseInt(prompt("Enter your numbers: ")); 
 
     s += nums[i]; 
 
     document.write("Number you chose: " + nums[i] + "<br />"); 
 
    } 
 
    var ave = s/N; 
 
    document.write("Average: " + ave + "<br />"); 
 
</script>

此外,如果你不需要這些輸入數字,你可以刪除nums數組,並使用一個簡單的變量來代替它:

<script type="text/javascript"> 
 
\t var nums = new Array(); 
 
\t var N = prompt("How many numbers would you like to enter? "); 
 
\t N = parseInt(N); 
 
\t var i = 0, s = 0; 
 
\t for(i = 0; i <= N - 1; i++) 
 
\t { 
 
\t \t var x = parseInt(prompt("Enter your numbers: ")); 
 
\t \t s += x; 
 
\t \t document.write("Number you chose: " + x + "<br />"); 
 
\t } 
 
\t var ave = s/N; 
 
\t document.write("Average: " + ave + "<br />"); 
 
</script>

0
<script> 
     var nums = new Array(); 
     var N = prompt("How many numbers would you like to enter? "); 
     N = parseInt(N); 
     var i = 0; 
     for (i = 0; i <= N - 1; i++) { 
      // convert string to integer 
      nums[i] = parseInt(prompt("Enter your numbers: ")); 
      document.write("Number you chose: " + nums[i] + "<br />"); 
     } 

     var sum = nums.reduce(function(prev, cur) { 
      return prev + cur; 
     }, 0); 

     var avg = sum/N; 
     console.log(avg); 
</script> 
0

入住此...

<title> 
    Title</title> 
<script type="text/javascript"> 
    function getSum(total, num) { 
    return total + num; 
    } 

    var nums = new Array(); 
    var N = prompt("How many numbers would you like to enter? "); 
    N = parseInt(N); 
    var i = 0; 

    for (i = 0; i <= N - 1; i++) { 
    nums[i] = parseInt(prompt("Enter your numbers: ")); 
    document.write("Number you chose: " + nums[i] + "<br />"); 

    } 
    document.write("Avg. of entered numbers = " + (nums.reduce(getSum))/nums.length); 

</script> 

<body> 
</body> 

檢查這個小提琴https://jsfiddle.net/45ab1812/1/

+0

這給出了輸入數字的總和不是平均值。 – Bhargav

+0

糟糕。謝謝 。更新我的答案和小提琴 – Harshal

0

首先,你可以在你的定義var i = 0 for循環, i <= N - 1i < N相同。編寫循環的更簡單的方法是...... for(var i = 0; i < N; i++)(也從上面刪除var N = 0;)。

現在沒有必要將用戶的數字放到數組中以獲得它們的平均值。所有你需要的平均值是數字和數字的總和。

JavaScript的另一個很酷的事情是,它可以自由地將您正在使用的變量轉換爲適合所使用上下文的變量類型。這意味着N = parseInt(N);在您的情況下不是必需的,因爲它只能被視爲循環中的整數。

如果考慮到上述兩個注意事項,可以使用以下代碼之一。

解決方案1:無陣列,只是保持數字之和量

//Prompt the user how many numbers they would like to enter 
var N = prompt("How many numbers would you like to enter? "); 

//Set sum to 0, which gives us a base to add each number to it. 
var sum = 0; 

//Complete this loop the same amount of times as the user entered in the prompt 
for (var i = 0; i < N; i++) { 

    //Add the users response to the sum of responses 
    sum += parseInt(prompt("Enter your numbers: ")); 
} 

//Write into the document the sum of 
document.write("Avg. of entered numbers = " + sum/N); 

但是,如果你真的想使用數組你的問題的狀態,你可以使用下面的代碼。

解決方案2:數組,同時保持和

//Prompt the user how many numbers they would like to enter 
var N = prompt("How many numbers would you like to enter? "); 

//Create an empty array to push elements to it 
var nums = new Array(); 

//Set sum to 0, which gives us a base to add each number to it. 
var sum = 0; 

//Complete this loop the same amount of times as the user entered in the prompt 
for (var i = 0; i < N; i++) { 

    //Get the users response to a variable 
    var response = parseInt(prompt("Enter your numbers: ")); 

    //Add that variable to the sum 
    sum += response; 

    //And also push that variable to the array 
    nums.push(response); 
} 

//Write into the document the sum of the document 
document.write("Avg. of entered numbers = " + sum/nums.length); 

的跟蹤如果您發現有我們用nums.length代替N。如果您遇到不確定用戶輸入多少個數字的情況,則可以使用數組長度來查看輸入的數量。

現在,您可以在不使用for循環中的數組總和的情況下使用數組的同時執行更簡單的方法。這利用了許多其他答案已經使用的reduce函數。

解決方案3:只是一個數組!

//Prompt the user how many numbers they would like to enter 
var N = prompt("How many numbers would you like to enter? "); 

//Create an empty array to push elements to it 
var nums = new Array(); 

//Complete this loop the same amount of times as the user entered in the prompt 
for (var i = 0; i < N; i++) { 

    //Push users response to the original array 
    nums.push(parseInt(prompt("Enter your numbers: "))); 

} 

//Use the reduce function to get the sum of the array, and then divide it by the amount of elements in the array. 
document.write("Avg. of entered numddbers = " + (nums.reduce((a, b) => a + b)/nums.length));