如何将函数中声明的数组用于另一个函数而不传递它

时间:2015-09-01 17:54:21

标签: javascript

我有两个功能。

第一个将计算数组的平均值,并返回一个平均值,最大值和最小值的数组。

第二个函数从表中的td获取一个数组,并将其传递给calcMean函数。 我遇到的问题是如何将值mean,max,min(数组中的元素值)放入该函数中。

当前代码:

function calcMean(nums){
  var num = 0;

  for (var i=0; i<nums.length; i++){
    num += parseFloat(nums[i]);
  }

  var divide = num/nums.length;

  mean = (parseInt(divide,10));

  var maxi = Math.max.apply(Math,nums);
  var mini = Math.min.apply(Math,nums);

  return[mean,maxi,mini];
}

function solution(){
  var str=new Array(6);
  str=document.getElementById("numbers").innerHTML;
  var arr = str.split(",");
  //alert(arr); i saw the array
  //alert(typeof arr); arr is object
  calcMean(arr);
}

3 个答案:

答案 0 :(得分:1)

您在calcMean(arr);函数返回数组中调用solution()函数,因此您可以将数组存储在变量中,如下所示:

 var result = calcMean(arr);

 alert(result[0]);  //return mean value
 alert(result[1]);  //return maxi value
 alert(result[2]);  //retirn mini value

希望这有帮助。

答案 1 :(得分:1)

如果您想获得#include<iostream> #include<vector> #include<algorithm> using namespace std; // factorial (x!) unsigned long long fact(unsigned char x) { unsigned long long res = 1; while(x) { res *= x; x--; } return res; } //binomial coefficient (n choose k) unsigned long long binom(unsigned char n, unsigned char k) { if(!n || !k) return 1; return (fact(n) / fact(k)) / fact(n-k); } //just for convenience template<class T> void printvector(std::vector<T> data) { for(auto l : data) cout << l << " "; cout << endl; } std::vector<unsigned long long> calculate(std::vector<int> data, int k) { std::vector<unsigned long long> res(k, 0); //result data std::vector<unsigned long long> multiplier(data.size(), 1); if(k < 1 || k > 255 || data.size() < 1) return res; //invalid stuff std::sort(data.begin(), data.end()); //as described for(int column = 0; column < k; column++) //each column separately { //count what to multiply to the multiplier array later std::vector<unsigned long long> newmultiplier(data.size(), 0); //for each array element in this column for(int x = column; x <= (data.size() + column - k); x++) { //core calculation res[column] += data[x] * multiplier[x] * binom(data.size() - x - 1, k - column - 1); //counting the new multiplier factor for(int helper = x + 1; helper < data.size(); helper++) newmultiplier[helper]++; } //calculating new multiplier for(int x = 0; x < data.size(); x++) { if(newmultiplier[x]) multiplier[x] *= newmultiplier[x]; } } return res; } int main() { printvector(calculate({1,2,3}, 2)); //output 4 8 return 0; } 返回的数组的平均值:

clacMean(arr)

答案 2 :(得分:-1)

您可以将mean,max和min val声明为global,并在calcMean函数调用时赋值,然后您可以获取此值。见下文:

            var maxi=new Array();
            var mini =new Array();
            var mean;
            function calcMean(nums){
            var num=0;
            mean=0,maxi=new Array(),mini =new Array();
            for (var i=0;i<nums.length;i++){
            num += parseFloat (nums[i])}
            var divide=num/nums.length;

            mean=(parseInt(divide,10));
             maxi = Math.max.apply(Math,nums);
             mini = Math.min.apply(Math,nums);

            }

            function solution(){
            var str=new Array(6);
            str=document.getElementById("numbers").innerHTML;
            var arr = str.split(",");
            calcMean(arr);
            alert(mean);
            alert(mini);
            alert(maxi);
            }