为什么我减少基于平均函数的返回喃? [英] Why does my reduce based average function return NaN?

查看:111
本文介绍了为什么我减少基于平均函数的返回喃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图得到一个阵列的平均

Trying to get the average of an array.

Array.prototype.average = function() {
    var sum = 0;
    this.reduce(function(a, b) {
        sum = a + b;
    });
    return sum / this.length;
};

[2, 15, 7].average();

为什么在平均函数调用返回 NaN的

推荐答案

您的程序没有工作,因为 A 已经从previous功能的累加值呼叫。在第一时间,将被使用的数组的前两个值。因此,将成为 17 2 + 15 )。既然你是不是从函数返回任何东西,未定义将被退回,在默认情况下,这将被用作值 A ,在未来的呼叫。因此,评价是这样的

Your program didn't work because, a has the accumulated value from the previous function call. The first time, first two values of the array will be used. So sum will become 17 (2 + 15). Since you are not returning anything from the function, undefined will be returned, by default, and that will be used as the value for a, in the next call. So, the evaluation goes like this

a: 2,          b: 15   => 17
a: undefined,  b: 7    => NaN

因此​​, NaN的,因为未定义+ 7 使得它如此。在 NaN的任意数字操作,总会给 NaN的,这就是为什么为NaN /本。长度,为您提供了 NaN的。你可以修改你的程序,只需通过返回总和当函数被调用,这样在下一次函数调用 A 将有适当的累计值。

So, sum will have NaN, since undefined + 7 makes it so. Any numeric operation on NaN, will always give NaN, that is why NaN / this.length, gives you NaN. You can fix your program, just by returning the current value of sum whenever the function is called, so that on the next function call, a will have proper accumulated value.

Array.prototype.average = function() {
    var sum = 0;
    this.reduce(function(a, b) {
        sum = a + b;
        return sum;
    });
    return sum / this.length;
};

但我们尚未充分利用的强大功能和灵活性的减少在这里。下面是使用时要考虑两个重要的点减少

But we are not making use of the power and flexibility of reduce here. Here are two important points to consider when using reduce.


  1. 减少 接受它说要使用的初始值的第二个参数。只要有可能,指定。

  1. reduce accepts a second parameter which says the initial value to be used. Whenever possible, specify that.

在传递给功能降低第一个参数积累的结果,将最后返回,利用这一点。无需使用单独的变量来跟踪结果。

The first parameter in the function passed to reduce accumulates the result and that will be returned finally, make use of that. No need to use a separate variable to keep track of the results.

所以你的code是这样的好

So your code would look better like this

Array.prototype.average = function() {

    var sum = this.reduce(function(result, currentValue) {
        return result + currentValue
    }, 0);

    return sum / this.length;

};

console.log([2, 15, 7].average());
# 8

减少其实是这样工作的。它遍历通过阵列和传递电流值作为第二个参数的函数和电流累积结果作为第一个参数,并从该函数返回的值将存储在累积值。所以,总和居然发现这样

reduce actually works like this. It iterates through the array and passes the current value as the second parameter to the function and the current accumulated result as the first parameter and the value returned from the function will be stored in the accumulated value. So, the sum is actually found like this

result: 0 , currentValue: 2   => 2    (Initializer value `0`)
result: 2 , currentValue: 15  => 17
result: 17, currentValue: 7   => 24

由于它跑出值从数组, 24 将返回结果的减少,这将被存储在

Since it ran out of values from the array, 24 will be returned as the result of the reduce, which will be stored in sum.

这篇关于为什么我减少基于平均函数的返回喃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆