Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取Javascript node.js中多个函数输出的总和_Javascript_Node.js - Fatal编程技术网

获取Javascript node.js中多个函数输出的总和

获取Javascript node.js中多个函数输出的总和,javascript,node.js,Javascript,Node.js,我有一个名为numFunc()的函数,它生成一个数字输出。 我想运行这个函数5次,得到所有5个输出的总和 这是我试过的 function total(){ for (itr=1; itr<=5; itr++) //run the function 5 times { var numF = numFunc(); //store the output from the function in a variable var sum = sum+numF; //Get the sum of

我有一个名为
numFunc()
的函数,它生成一个数字输出。 我想运行这个函数5次,得到所有5个输出的总和

这是我试过的

function total(){
for (itr=1; itr<=5; itr++) //run the function 5 times
{
  var numF = numFunc(); //store the output from the function in a variable
  var sum = sum+numF; //Get the sum of all 5 variables
}
console.log(sum);

}
total();

我想要的是作为单个值的总和。如何实现这一点?

您需要在循环外部声明变量,并将其设置为数值,否则将向整数添加未定义的变量。感谢@jfriend00的澄清

function total(){
    var sum = 0;
    for (itr=1; itr<=5; itr++) //run the function 5 times
    {
      var numF = numFunc(); //store the output from the function in a variable
      sum = sum + numF; //Get the sum of all 5 variables
    }
    console.log(sum);

}
total();
函数总数(){
var总和=0;
对于语句中的(itr=1;itr:

var sum = sum + numF;
您正试图添加一些尚不存在的内容,因为您刚刚用
var

在语句中,应该在循环上方声明变量:

function total(){
    // declaring the variables before you use them.
    // I will set them to 0 so I know they're supposed to be numbers, and 0 wont affect the outcome of the function
    var numF = 0;
    var sum = 0;        

    for (itr=1; itr<=5; itr++) //run the function 5 times
    {
      numF = numFunc(); //store the output from the function in a variable
      sum = sum+numF; //Get the sum of all 5 variables
    }
    console.log(sum);

}
total();
函数总数(){
//在使用变量之前声明它们。
//我会将它们设置为0,这样我知道它们应该是数字,0不会影响函数的结果
var-numF=0;
var总和=0;

对于(itr=1;itr如何尝试一个数组,然后在函数触发后像这样从外部添加该数组值

编辑:使用repl.it代码编辑器来测试这个理论。我不得不稍微更改代码,但保持不变。使用分离函数,然后将数组生成器函数输入计算器函数,以正确计算数组变量。这里的repl链接:()

函数numFunc(){
返回3;
};
函数总数(){
var toBeSummed=[];//设置一个数组,将求和变量作为一种更通用的变量类型放入,以便在中执行此操作。

对于(var-itr=0;itr谢谢大家的帮助。 我想我发现了这个问题。原来的numFunc()函数似乎有一个console.log()语句,这就是为什么我一直在获取3的列表,并用一个返回来替换它,从而解决了这个问题


正如你们建议的,在循环外部声明sum变量会阻止显示'NaN'

for
外部声明变量
sum
。code:
函数total(){var sum=0;for(itr=1;itr问题是
sum
for
循环之前从未初始化为零,因此
sum+numF
sum
未首先初始化时不起作用。您的解决方案会起作用,但对原因的解释并不正确。仅供参考,实际的
var sum
将被提升到乐趣的顶部函数total(){var sum=0;var numF=0;for(itr=1;itr)但是我仍然得到相同的结果。这段代码工作。你的numFunc()看起来像什么?另外,为了使它更健壮,我将“for”改为“for”(var itr=1;itr感谢Mike,但您不认为第(7)行:'toBeSummed[arrayItr]=numF;对于每个arrayItr 0-5使数组值等于numF值'应该在某处注释:)创建重复的“numFunc”以仅返回数字“x”次。“total”函数从您的函数构造一个数组。“counter”函数(我知道命名不好)使用数组“toBeSummed”和for-in-loop和“x”数字
function total(){
    // declaring the variables before you use them.
    // I will set them to 0 so I know they're supposed to be numbers, and 0 wont affect the outcome of the function
    var numF = 0;
    var sum = 0;        

    for (itr=1; itr<=5; itr++) //run the function 5 times
    {
      numF = numFunc(); //store the output from the function in a variable
      sum = sum+numF; //Get the sum of all 5 variables
    }
    console.log(sum);

}
total();
function numFunc(){
    return 3;
};

function total(){
    var toBeSummed = []; //set up an array to put your sum variables into as a more versatile variable type to perform this in.
    for (var itr=0; itr<5; itr++){ //runs the function 5 times 
        var numF = numFunc(); //store the output from the function
        var arrayItr = itr; //uses "itr" variable to iterate a 0 based index 5 times
        toBeSummed[arrayItr] = numF; //for each arrayItr 0-5 makes the array value equal to the numF value.
    };
    return toBeSummed;
};

var sum = 0;

function counter(totals){
    for(sums in totals){ //iterates the x array elements(5 in this case)
        sum = sum + totals[sums]; // Adds the current value of sum, plus the values at each existing array point(at this point values in array index 0-5)
        //console.log(sum); //Should now log the correct set of sums "browser" 
    };
    console.log(sum);
};


counter(total());