0

乱数を使用して配列に、次にテーブルに、単純な数学の問題を含むいくつかの関数を呼び出そうとしています。私は近いと思いますが、表の書き方がわかりません。私は次のように持っています:

HTML:

</head>
<body>
<div>
    <table id="content">
    </table>
</div>
</body>
</html>

脚本:

function RandX(){

var x = Math.floor(Math.random()*300);
}

function RandY(){                       
var y = Math.floor(Math.random()*300);
}



var math1 = function(x,y) {return x * y;};  
var math2 = function(x,y) {return x + 1;};
var math3 = function(x,y) {return x/y;};
var math4 = function(x,y) {return x % y;};
var math5 = function(x,y) {return math.sqrt(x^2 + y^2);};
var math6 = function(x,y) {return x^2;};
var math7 = function(x,y) {return (10*x) + (y/2);};
var math8 = function(x,y) {return SIGMA)

var maths = [];

maths.push(math1);
maths.push(math2);
maths.push(math3);
maths.push(math4);
maths.push(math5);
maths.push(math6);
maths.push(math7);
maths.push(math8);
//maths[0](RandX,2);//return RandX* RandY
//maths[1](3,4);//12 - put random numbners here?



 var c = document.getElementById("Content");
 var content = document.createElement("tr");
 for( var i = 0; i < maths.length; i++ ){
  var d = document.createElement("<td>" + "</td>);
  d.innerHTML = "<td>" + maths[0](RandX(), RandY()) + "</td>"; 
  content.appendChild(d);
 }
 c.appendChild(content);

ありがとうございました!

4

4 に答える 4

0

これらの関数は値を返す必要があります。これらは名前以外は同一であるため、2 つの関数にはあまり意味がないようです。

function RandX(){
  return Math.floor(Math.random()*300);
}

function RandY(){                       
  return Math.floor(Math.random()*300);
}

変数に割り当てられてから配列にプッシュされる関数は、配列リテラルで簡単に割り当てることができます。

var maths = [
    function(x,y) {return x * y;},
    function(x,y) {return x + 1;},
    function(x,y) {return x/y;},
    function(x,y) {return x % y;},
    function(x,y) {return math.sqrt(x^2 + y^2);},
    function(x,y) {return x^2;},
    function(x,y) {return (10*x) + (y/2);},
    function(x,y) {return SIGMA)
];

 var c = document.getElementById("Content");
 var content = document.createElement("tr");
 for( var i = 0; i < maths.length; i++ ){

   // The argument for createElement is the tagname, not markup
   var d = document.createElement('td');

TD を TD のコンテンツとして割り当てることはできません。これは単なるテキストなので、テキスト ノードを作成して挿入することをお勧めします。また、毎回異なる関数を呼び出したいので、次を使用しますi

   d.appendChild(document.createTextNode(maths[i](RandX(), RandY()))); 
 }

// In IE, you must append rows to a table section element, (thead, tfoot or tbody):
c.tBodies[0].appendChild(content);

HTH。

于 2013-03-28T00:49:09.703 に答える
0

これが役立つことを願っています!

http://jsfiddle.net/Jra9w/1/

//You don't need 2 different functions to execute the same logic, so here
//RandX and RandY are combined into randNum
function randNum() {

    return Math.floor(Math.random() * 300);

}

//Instead of instanciating the array and then pushing the functions into 
//it you can instanciate the array already initialized with a set of items.
var maths = [
        function (x, y) { return x * y; },
        function (x, y) { return x + 1; },
        function (x, y) { return x / y; },
        function (x, y) { return x * y; },
        function (x, y) { return Math.sqrt(x^2 + y^2); },
        function (x, y) { return x^2; },
        function (x, y) { return (10 * x) + (y / 2); },
        function (x, y) { 
            //SIGMA is not defined in your code and I am
            //not sure what you want to do in here. This function will throw
            //an error if you try to return the undeclared SIGMA variable

            //return SIGMA;
        }
    ],
    i = 0,
    len = maths.length,

    //JavaScript is a case-sensitive language so you must write the id exactly as defined
    //in the id attribute of the html element
    tableElement = document.getElementById('content'),
    results = [];

//Note that we have cached the maths.length result into the 'len' variable instead of
//putting it directly into the loop condition. The reason for this is that the condition //will be evaluated on each iterations and property lookups are expensive in JavaScript, so 
//we try to reduce them.

for (; i < len; i++) {
    //we push all the results into a new array
    results.push(maths[i](randNum(), randNum()));
}

//Array.join is faster for concatenating strings that using the + operator when you want to 
//concatenate a lot of strings together.
tableElement.innerHTML = '<tr><td>' + results.join('</td><td>') + '</td></tr>';

于 2013-03-28T01:15:52.343 に答える
0

アイテムをループして、次のようにテーブルに追加できます。

var contentDiv = document.getElementById("Content");
var row = document.createElement("tr");
for( var i = 0; i < maths.length; i++ ){
    var cell = document.createElement("td");
    cell.innerHTML = maths[i]; 
    content.appendChild(cell);
}
contentDiv.appendChild(content);
于 2013-03-28T00:27:15.340 に答える
0

スクリプトにいくつかのエラーがあります。これが正しいエラーです。

http://jsfiddle.net/wMF5M/

function RandX(){
var x = Math.floor(Math.random()*300);
    return x;
}

function RandY(){                       
var y = Math.floor(Math.random()*300);
    return y;
}

var math1 = function(x,y) {return x * y;};  
var math2 = function(x,y) {return x + 1;};
var math3 = function(x,y) {return x/y;};
var math4 = function(x,y) {return x % y;};
var math5 = function(x,y) {return math.sqrt(x^2 + y^2);};
var math6 = function(x,y) {return x^2;};
var math7 = function(x,y) {return (10*x) + (y/2);};
var math8 = function(x,y) {return SIGMA;}

var maths = [];

maths.push(math1);
maths.push(math2);
maths.push(math3);
maths.push(math4);
maths.push(math5);
maths.push(math6);
maths.push(math7);
maths.push(math8);
//maths[0](RandX,2);//return RandX* RandY
//maths[1](3,4);//12 - put random numbners here?



 var content = document.getElementById("content");
 var tr = document.createElement("tr");
 for( var i = 0; i < maths.length; i++ ){
  var td = document.createElement('td');
  td.innerHTML =  + maths[0](RandX(), RandY());
  tr.appendChild(td);
 }
 content.appendChild(tr);
于 2013-03-28T00:29:24.597 に答える