简体   繁体   English

Javascript代码将偶数和奇数分开,然后获得各自的总和和平均值

[英]Javascript code to separate even and odd numbers then get their respective sums and averages

So I have this code that requires a user to specify the length of an array,store some values in the array then separate the odd and even numbers. 所以我有这段代码,要求用户指定数组的长度,在数组中存储一些值,然后将奇数和偶数分开。 I have done everything so far but i am getting some incorrect output. 到目前为止,我已经完成了所有操作,但是得到了一些不正确的输出。 I have attached my code. 我已附上我的代码。

 //Variable and array declaration var arrayNum = []; //Create an array with no size var arrayEven = []; //Array to hold even numbers var arrayOdd = []; //Array to hold odd numbers var i; //Variable to store the index of arrayNum() above var NUM_INPUTS; //Variable to store array size which is the number of elements var sumOdd = 0.0; //variable to store the sum if the odd numbers var sumEven = 0.0; //variable to store the sum if the odd numbers var avgO = 0.0; var avgE = 0.0; NUM_INPUTS = parseInt(prompt("Enter the number of inputs you need: ")); //Ask the user to specify size of the array //loop for entering values into the array for (i = 0; i < NUM_INPUTS; i++) { arrayNum.push(parseFloat(prompt("Enter the numbers: " + (i + 1)))); //Check whether a number is odd and add it to arrayOdd[] if ((arrayNum[i] % 2) === 1) { arrayOdd.push(arrayNum[i]); for (var x = 0; x < arrayOdd.length; x++) { sumOdd += arrayOdd[x]; } //calculate the sum of odd numers avgO = sumOdd / arrayOdd.length; } //Check whether a number is eve and add it to arrayEven[] else { arrayEven.push(arrayNum[i]); for (var y = 0; y < arrayOdd.length; y++) { sumEven += arrayEven[y]; } //sumEven+=arrayEven[i]; //calculate sum of even numbers avgE = sumEven / arrayEven.length; } } //Output results document.write("All numbers in the array are: " + arrayNum); document.write("<br/>All even numbers in the array are: " + arrayEven); document.write("<br/> The sum of all even numbers is: " + sumEven + " and average of the even numbers is: " + avgE); document.write("<br/>"); document.write("<br/>All odd numbers in the array are: " + arrayOdd); document.write("<br/> The sum of all odd numbers is: " + sumOdd + " and the average of the odd numbers is " + avgO); 

In cases like this, explain your code to a duck! 在这种情况下,请向鸭子解释代码! (No I'm not kidding, this is known as rubber duck debugging ) (不,我不是在开玩笑,这被称为橡皮鸭调试

The user enters 2 as the count of numbers he wants to insert. 用户输入2作为他要插入的数字计数。

The loop will iterate 2 times and ask the user for a number. 循环将迭代2次,并要求用户输入数字。

The user enters 1 as the first number. 用户输入1作为第一个数字。

1 is odd, therefore it gets pushed to arrayOdd , it also gets added to sumOdd which is 1 then. 1是奇数,因此将其推送到arrayOdd ,然后还将其添加到sumOdd ,然后为1

The user enters 3 as the second number. 用户输入3作为第二个数字。 As it is odd, it will also get pushed into arrayOdd . 奇怪的是,它也将被推入arrayOdd The inner loop runs again, iterates over arrayOdd , takes the first element ( 1 ) and adds it to sumOdd (!), which is 2 now, then it takes the second element ( 3 ) and adds it to sumOdd , sumOdd is 5. 内部循环再次运行,遍历arrayOdd ,获取第一个元素( 1 )并将其添加到sumOdd (!),现在为2,然后获取第二个元素( 3 )并将其添加到sumOddsumOdd为5。

But wait ... the sum of 1 and 3 is 4, not 5. Summing up all numbers of sumOdd and sumEven in the loop seems to be a mistake. 但是,请稍等... 1和3的总和是4,而不是5。对循环中的sumOddsumEven所有数字求和似乎是一个错误。

 for (var x = 0; x < arrayOdd.length; x++) {
  sumOdd += arrayOdd[x];
}

That has to be: 必须是:

 sumOdd += /* left as an exercise */;

You are doing following things wrong 您做错了事

  • You are adding all the previous values of arrayOdd and arrayEven again and again. 您一次又一次地添加arrayOddarrayEven所有先前值。 You should add the new value to the sumOdd and sumEven . 您应该将新值添加到sumOddsumEven
  • You are calculating average before all inputs are completed. 您正在计算所有输入完成之前的平均值。 You should calculate avgO and avgE at end of loop. 您应在循环结束时计算avgOavgE

 var arrayNum = []; var arrayEven = []; var arrayOdd = []; var i; var NUM_INPUTS; var sumOdd = 0.0; var sumEven = 0.0; var avgO = 0.0; var avgE = 0.0; NUM_INPUTS = parseInt(prompt("Enter the number of inputs you need: ")); for (i = 0; i < NUM_INPUTS; i++) { arrayNum.push(parseFloat(prompt("Enter the numbers: " + (i + 1)))); if ((arrayNum[i] % 2) === 1) { arrayOdd.push(arrayNum[i]); sumOdd += arrayNum[i]; } else { arrayEven.push(arrayNum[i]); sumEven += arrayNum[i]; } } avgO = sumOdd / arrayOdd.length; avgE = sumEven / arrayEven.length; //Output results document.write("All numbers in the array are: " + arrayNum); document.write("<br/>All even numbers in the array are: " + arrayEven); document.write("<br/> The sum of all even numbers is: " + sumEven + " and average of the even numbers is: " + avgE); document.write("<br/>"); document.write("<br/>All odd numbers in the array are: " + arrayOdd); document.write("<br/> The sum of all odd numbers is: " + sumOdd + " and the average of the odd numbers is " + avgO); 

The mistake I found is that you use oddnum arrays for calculating even numbers. 我发现的错误是您使用奇数数组来计算偶数。

for (var y = 0; y < arrayOdd.length; y++) {
      sumEven += arrayEven[y];
    }

 //Variable and array declaration var arrayNum = []; //Create an array with no size var arrayEven = []; //Array to hold even numbers var arrayOdd = []; //Array to hold odd numbers var i; //Variable to store the index of arrayNum() above var NUM_INPUTS; //Variable to store array size which is the number of elements var sumOdd = 0.0; //variable to store the sum if the odd numbers var sumEven = 0.0; //variable to store the sum if the odd numbers var avgO = 0.0; var avgE = 0.0; NUM_INPUTS = parseInt(prompt("Enter the number of inputs you need: ")); //Ask the user to specify size of the array //loop for entering values into the array for (i = 0; i < NUM_INPUTS; i++) { arrayNum.push(parseFloat(prompt("Enter the numbers: " + (i + 1)))); //Check whether a number is odd and add it to arrayOdd[] if ((arrayNum[i] % 2) === 1) { arrayOdd.push(arrayNum[i]); for (var x = 0; x < arrayOdd.length; x++) { sumOdd += arrayOdd[x]; } //calculate the sum of odd numers avgO = sumOdd / arrayOdd.length; } //Check whether a number is eve and add it to arrayEven[] else { arrayEven.push(arrayNum[i]); for (var y = 0; y < arrayEven.length; y++) { sumEven += arrayEven[y]; } //sumEven+=arrayEven[i]; //calculate sum of even numbers avgE = sumEven / arrayEven.length; } } //Output results document.write("All numbers in the array are: " + arrayNum); document.write("<br/>All even numbers in the array are: " + arrayEven); document.write("<br/> The sum of all even numbers is: " + sumEven + " and average of the even numbers is: " + avgE); document.write("<br/>"); document.write("<br/>All odd numbers in the array are: " + arrayOdd); document.write("<br/> The sum of all odd numbers is: " + sumOdd + " and the average of the odd numbers is " + avgO) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript - 奇数和偶数不能在我的代码上同步工作? - Javascript - odd and even numbers can't work synchronous on my code? 使用 for 循环将偶数和奇数推送到相应的 arrays - push even and odd numbers to respective arrays using a for loop Javascript:保持偶数和奇数随机数的数量并获得每个的总和 - Javascript: Keep count of even and odd random numbers and get the sum of each 如何将数字 1-20 放入数组中,然后将偶数和奇数分开并在 web 浏览器上打印 - How to get numbers 1-20 into an array, then separate even and odd numbers and print them on the web browser 如果其他数字是奇数,则返回“偶数”,而“奇数”其他数字是偶数 javascript - return “even” if others numbers are odd and “odd” the others number are even javascript 使用余数运算符Javascript对奇数和偶数进行排序 - Sorting odd and even numbers with the remainder operator Javascript Javascript函数检查偶数/奇数 - Javascript function to check for even/odd numbers 使用函数对奇数和偶数进行排序Javascript - using a function to sort odd and even numbers Javascript 取数组并将奇数和偶数推入单独的数组 - Take array and push odd and even numbers into separate arrays 从 100 倒计时到数字 (N) 并分开偶数和奇数。 第一行显示偶数,第二行显示奇数 - Countdown from 100 to the number (N) and separate even and odd numbers. Show even numbers in the first line and odd numbers in the second line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM