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

时间:2019-03-03 10:29:56

标签: javascript arrays

所以我有这段代码,要求用户指定数组的长度,在数组中存储一些值,然后将奇数和偶数分开。到目前为止,我已经完成了所有操作,但是得到了一些不正确的输出。我已经附上我的代码。

//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);

3 个答案:

答案 0 :(得分:1)

在这种情况下,向鸭子解释代码! (不,我不是在开玩笑,这就是橡皮鸭调试

用户输入2作为他要插入的数字计数。

循环将迭代2次,并要求用户输入数字。

用户输入1作为第一个数字。

1是奇数,因此将其推送到arrayOdd,然后还将其添加到sumOdd的{​​{1}}中。

用户输入3作为第二个数字。奇怪的是,它也将被推送到1中。内部循环再次运行,遍历arrayOdd,获取第一个元素(arrayOdd)并将其添加到1(!),现在为2,然后获取第二个元素( sumOdd)并将其添加到3sumOdd为5。

但是等等... 1和3的总和是4,而不是5。将循环中sumOddsumOdd的所有数字加起来似乎是一个错误。 em>

sumEven

必须是:

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

答案 1 :(得分:0)

您做错了以下事情

  • 您要一次又一次地添加arrayOddarrayEven的所有先前值。您应该将新值添加到sumOddsumEven
  • 您正在计算所有输入完成之前的平均值。您应在循环结束时计算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);

答案 2 :(得分:0)

我发现的错误是您使用奇数数组来计算偶数。

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)