Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i make a function to claculate the mean of a random 6-item array, the random array copied to a div in table , i make a function to get a random array
now i want to read this saved array and call the function calcMean ()
and take the mean value and the min also the max of the array

HTML
    <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"><div  id="numbers"></div>
      </td>
       </tr>
    <tr>
     <td colspan="3"> </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> </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>


in JS
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;
   
}




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 solution (){

//read the array which saved in div id="numbers"
//call calcMean();
//get the mean ??
//get the min and max ??
}
Posted
Updated 1-Sep-15 3:40am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900