2013-03-28 8 views
0

배열에 임의의 숫자를 사용하여 간단한 수학 문제를 포함하는 여러 함수를 호출하려고합니다. 나는 내가 가깝다고 생각하는데, 나는 그 테이블을 쓰는 방법을 확신 할 수 없다. 다음과 같이 내가 가진 :배열에서 테이블로 함수 호출하기

HTML :

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

SCRIPT :

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); 

감사합니다!

+0

나는 BTW – andrsnn

+0

은 * 랜드 * 함수는 값을 반환하지 않는 SIGMA을 댓글을 달았 shouild. 또한 잘못된 HTML 인 TD의 내용으로 TD를 삽입하려고합니다. 마지막으로 innerHTML을 사용하여 IE에서 표를 수정할 수 없으며 전체 표나 셀의 내용 만 작성해야합니다. – RobG

+0

IE에서 전혀 기울이지 않습니까? 고마워요. – andrsnn

답변

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); 
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); 
+0

gotcha! 나는 그것이 지금 어떻게되는지에 관해 안다, 고마워! – andrsnn

+0

I'de 나는 형식 오류가 계속 ^^ – Adidi

+0

다음 표시하기가 행복 : 컨텐츠가 null [이 오류에 브레이크] \t content.appendChild (TR) – andrsnn

0

이 함수는 값을 반환해야합니다. 그들이 이름 이외 동일하므로, 두 가지 기능의 많은 점없는 것 :

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를.

+0

나는 createtextnode를 사용하지 않았지만 나는 그것을 고맙게 생각할 것입니다! – andrsnn

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>'; 

+0

아하챠! 고맙습니다!! :디 – andrsnn