Javascript Sum和Average奇怪的问题

时间:2016-12-19 13:05:31

标签: javascript arrays

嘿伙计们,我有一个javascript数组的奇怪问题,我想计算它的总和和平均值。

所以我有一个输入提示,我要求用户输入一些数字。



SELECT `movies`.`title` as `title`, `movies`.`description` as [...],
`directors`.`name` as `name` [...], `composers`.`name` as `cname` [...]
from
    `movies`
left join
    `directors` on `movies`.`directors_id`=`directors`.`id`
left join
    `movies`.`musicians_id` = `musicians`.`id` [...]




因此,从控制台日志中我得到以下打印

var sums = false;
var numbersArray = [];

function calculateAverage() {
  while (sums == false) {
    var numbers = prompt("Enter Integers.", "");
    if (numbers != null) {
      console.log(numbers);
      numbersArray.push(numbers);


    } else {
      var sum = 0;
      console.log(numbersArray);
      for (var i = 0; i < numbersArray.length; i++) {
        sum += numbersArray[i];
      }

      var avg = sum / numbersArray.length;
      console.log(avg);
      sums = true;
    }
  }
}

 calculateAverage();

这怎么可能?

好吧,我的坏!要接受第一个答案,因为你们都是正确的!

9 个答案:

答案 0 :(得分:3)

在推入数组之前将字符串转换为整数

使用parsInt

numbersArray.push(parseInt(numbers));

var sums = false;
var numbersArray = [];

function calculateAverage() {
  while (sums == false) {
    var numbers = prompt("Enter Integers.", "");
    if (numbers != null) {
      console.log(numbers);
      numbersArray.push(parseInt(numbers));


    } else {
      var sum = 0;
      console.log(numbersArray);
      for (var i = 0; i < numbersArray.length; i++) {
        sum += numbersArray[i];
      }

      var avg = sum / numbersArray.length;
      console.log(avg);
      sums = true;
    }
  }
}

calculateAverage();

答案 1 :(得分:3)

在添加

之前做一下parseInt

var sums = false;
var numbersArray = [];

function calculateAverage() {
  while (sums == false) {
    var numbers = prompt("Enter Integers.", "");
    if (numbers != null) {
      console.log(numbers);
      numbersArray.push(numbers);


    } else {
      var sum = 0;
      console.log(numbersArray);
      for (var i = 0; i < numbersArray.length; i++) {
        sum += parseInt(numbersArray[i]);
      }

      var avg = sum / numbersArray.length;
      console.log(avg);
      sums = true;
    }
  }
}

 calculateAverage();

答案 2 :(得分:2)

sum += numbersArray[i];

应该是

sum += parseInt(numbersArray[i]);

因为没有它你会在你的字符串中添加另一个字符。

答案 3 :(得分:2)

在推入数组

之前将用户输入的变量转换为整数
numbersArray.push(+ numbers);

答案 4 :(得分:2)

  

这怎么可能?

现在你正在处理strings数组,因此行为。

将字符串转换为数字值,这应该是固定的。要将字符串更改为数字,请使用parseInt()

numbersArray.push(parseInt(numbers,10));

答案 5 :(得分:2)

在添加之前,使用public class PlanListAdapter extends ArrayAdapter { private String[] titles; private Context context; private View decorView; public PlanListAdapter(Context context, View decorView, String[] names) { super(context, -1); this.context = context; this.titles = names; this.decorView = decorView; } @NonNull @Override public View getView(int position, View convertView, ViewGroup parent) { View rootView = parent.getRootView(); View itemView = LayoutInflater.from(context).inflate(R.layout.layout_planitem, null, false); BlurView blurView = (BlurView) itemView.findViewById(R.id.blur_item); blurView.setupWith(rootView) .windowBackground(decorView.getBackground()) .blurAlgorithm(new RenderScriptBlur(context, true)) .blurRadius(24f); return itemView; } @Override public int getCount() { return titles.length; } } parseFloat方法将字符串转换为nuber。请找到附带的代码。

parseInt

答案 6 :(得分:2)

你需要添加parseInt来对整数求和,否则4,5,1将加入形成451并且平均451/3 = 150.33333

&#13;
&#13;
var sums = false;
var numbersArray = [];

function calculateAverage() {
  while (sums == false) {
    var numbers = prompt("Enter Integers.", "");
    if (numbers != null) {
      console.log(numbers);
      numbersArray.push(numbers);


    } else {
      var sum = 0;
      console.log(numbersArray);
      for (var i = 0; i < numbersArray.length; i++) {
        sum += parseInt(numbersArray[i]);
      }

      var avg = sum / numbersArray.length;
      console.log(avg);
      sums = true;
    }
  }
}
window.onload = calculateAverage();
&#13;
&#13;
&#13;

答案 7 :(得分:2)

需要使用ParseInt()

var sums = false;
var numbersArray = [];
calculateAverage();

function calculateAverage() {
  while (sums == false) {
    var numbers = prompt("Enter Integers.", "");
    if (numbers != null) {
      console.log(numbers);
      numbersArray.push(numbers);


    } else {
      var sum = 0;
      console.log(numbersArray, numbersArray.length);
      for (var i = 0; i < numbersArray.length; i++) {
        sum += parseInt(numbersArray[i]);

      }
      console.log(sum);
      var avg = sum / numbersArray.length;
      console.log(avg);
      sums = true;
    }
  }
}

答案 8 :(得分:0)

我看到您已经找到了解决方案,但是我一直在使用一个名为“快捷方式”的简单软件包。您可以使用const short = require('@testgrandma/shortcuts');进行安装,然后就可以使用此脚本

short.arrayave([array]);

它将返回数组的平均值。

https://www.npmjs.com/package/@testgrandma/shortcuts