如何在javascript中将值从两个不同的函数传递给第三个函数?

时间:2015-09-01 09:36:15

标签: javascript html

我正在尝试使用javascript在html页面中编写练习,以计算从randomArray()函数生成的6个数字的随机数组的平均值。 在第一次加载页面后,当我点击“新问题”按钮时,此函数被调用以将随机数组复制到表格的单元格中。

我写calcMean来计算从randomArray()传递的随机数组的平均值,当我输入输入时,当我按下Enter键时,我会停止刷新页面,并返回searchm()输入的值 但现在的问题是,我想把mean,maxi,mini和inans转换到另一个函数来比较真正的答案和用户的答案,如果条件是肯定的,那就写了一个div。 第二个问题是,如果我点击调用calcMean()方法的“解决方案”按钮,我想从solution()取平均值以在输入上显示它,我必须传递给最后一个函数去写。

<div >
<form  action="" method="post" name="meanForm"  onsubmit='return false'     id="formmine">

<table width="100%" border="0" >
 <tr>
  <td colspan="3" style="background-color:#06F ;color:#FFF">Answer this   problem</td>
    </tr>
 <tr>
 <td style="color:green; font-size:20px">What is the mean of these numbers </td>
    <td colspan="2" id="numbers">
      </td>
       </tr>
    <tr>
     <td colspan="3">&nbsp;</td>
    </tr>
  <tr id="answerANDpic">
    <td height="62" colspan="3"  align="center" > <input name="" type="text"      size="15" maxlength="100" height="50" style=" border: solid #0C0 ; border-width:thin"  id="answer"   onkeydown="searchm(this)"/> </td>
      </tr>
   <tr>
     <td colspan="3"  ><div id ="explain" ></div></td>
  </tr>
 <tr>
    <td>&nbsp;</td>
    <td><input name="" type="button" id="newEx" style="background-color:green ; color:white"  align ="left" value="New Problem" class="send_feed"  onclick="randomArray(6,0,99)" /></td>
     <td><input name="" type="button"  id="solution" style="background-color:#606 ; color:#FFF  " align="left" class="send_feed" value="Solution"  onclick="solution()"/></td>
    </tr>
</table>



</form>
</div>

但是在javascript中

var myNumArray = randomArray(6,0,99);

function random_number(min,max) {
    return (Math.round((max-min) * Math.random() + min));
}

function randomArray(num_elements,min,max) {
   var nums = new Array;

   for (var element=0; element<num_elements; element++) {
       nums[element] = random_number(min,max);
    }

    document.getElementById("numbers").innerHTML=nums;
    calcMean(nums);
}




function calcMean(nums) {
    var num=0;
    for (var i=0;i<nums.length;i++) {
        num += parseFloat( nums[i], 6 );
    }
    var divide=num/nums.length;
    var mean=(parseInt(divide,10));
    var maxi = Math.max.apply(Math,nums);
    var mini = Math.min.apply(Math,nums);

    return mean,maxi,mini;
}

function searchm(ele) {
    if(event.keyCode == 13) {
        // alert(ele.value); // i get the value and put it on alert        

        var inans= ele.value;
        return inans;
    }
}

function comparing(mean,maxi,mini,inans) {
    if(inans==mean){document.getElementById("explain").innerHTML=  "correct"+","+inans+"," +maxi+","+mini;
    }
}

function solution() {
   //some code to take the mean value(realvalue)from calcMean()
   //what is parameter should i pass it when i click on solution button to     pass it this function
}

3 个答案:

答案 0 :(得分:0)

了解Objects https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

使用对象可以从函数中返回任何类型的数据类型

例如

function calcMins (nums) {
// ...
  return {mean: mean, maxi: maxi, mini: mini}

var values = calcMins(nums)
var mean = values.mean // and so on

在解决方案()中,您需要阅读document.getElementById("numbers").innerHTML并运行 已保存的号码calcNums(nums)。之后,您可以将values.mean与输入平均值进行比较

没有编写完整的代码,导致它的练习,发现如何使用函数,对象或数组,似乎你熟悉数组

return [mean, maxi, mini]可以使用,也可以尝试

答案 1 :(得分:0)

你可以改成这样的东西。 正如其他用户所说,您无法返回多个值,因此将其转换为对象

var valuesReturned = null; // initialize variable

function randomArray(num_elements,min,max) {
   var nums = new Array;

   for (var element=0; element<num_elements; element++) {
       nums[element] = random_number(min,max);
    }

    document.getElementById("numbers").innerHTML=nums;
    valuesReturned = calcMean(nums);
}




function calcMean(nums) {
    var num=0;
    for (var i=0;i<nums.length;i++) {
        num += parseFloat( nums[i], 6 );
    }
    var divide=num/nums.length;
    var mean=(parseInt(divide,10));
    var maxi = Math.max.apply(Math,nums);
    var mini = Math.min.apply(Math,nums);

    return {"mean":mean, "maxi": maxi, "mini": mini}; //convert to an object
}

function solution() {
    if(valuesReturned) {
        // make your stuff
        // valuesReturned.mean
        // valuesReturned.mini
        // valuesReturned.maxi
    }
}

修改

适用于searchm()功能

function searchm() {
   if(event.keyCode == 13) {
      var values = calcMean();
      var inans= values.mini;
     return inans;
   }
}

答案 2 :(得分:0)

 Declare a new array in calcMean function and push all mean , mini , maxi  
 value in new array. And get these value in solution like this :
            var myNumArray = randomArray(6,0,99);
            var numarr=new Array();
            function random_number(min,max) {
                return (Math.round((max-min) * Math.random() + min));
            }

            function randomArray(num_elements,min,max) {
               var nums = new Array;

               for (var element=0; element<num_elements; element++) {
                   nums[element] = random_number(min,max);
                }

                document.getElementById("numbers").innerHTML=nums;
               numarr= calcMean(nums);
            }




            function calcMean(nums) {
                var num=0;
                for (var i=0;i<nums.length;i++) {
                    num += parseFloat( nums[i], 6 );
                }
                var divide=num/nums.length;
                var arr=new Array();
                var mean=(parseInt(divide,10));
                var maxi = Math.max.apply(Math,nums);
                var mini = Math.min.apply(Math,nums);
                arr.push(mean);
                arr.push(maxi);
                arr.push(mini);
                return arr;
            }

            function searchm(ele) {
                if(event.keyCode == 13) {
                    // alert(ele.value); // i get the value and put it on alert        

                    var inans= ele.value;
                    return inans;
                }
            }

            function comparing(mean,maxi,mini,inans) {
                if(inans==mean){document.getElementById("explain").innerHTML=  "correct"+","+inans+"," +maxi+","+mini;
                }
            }

            function solution() {
               alert(numarr[0]);   //for mean value
               alert(numarr[1]);    // for maxi
               alert(numarr[2]);   // for maxi
            }