[英]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
)并将其添加到sumOdd
, sumOdd
为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。对循环中的sumOdd
和sumEven
所有数字求和似乎是一个错误。
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 您做错了事
arrayOdd
and arrayEven
again and again. 您一次又一次地添加arrayOdd
和arrayEven
所有先前值。 You should add the new value to the sumOdd
and sumEven
. 您应该将新值添加到sumOdd
和sumEven
。 avgO
and avgE
at end of loop. 您应在循环结束时计算avgO
和avgE
。 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.