Friday, 15 February 2013

function - How can I get this loop's information in a table (Javascript) -



function - How can I get this loop's information in a table (Javascript) -

i'm trying loop in code after text have info outputted table. explain further, function function doevens() creates loop takes uses number person enters input text box type in number: <input type="number" id="num">, , displays numbers between number , 2 within paragraph: <p id="space"></p>, when numbers displayed within paragraph have semicolon , space separating them, instead of numbers within table, have tried using createelementbyid , no luck, have tried putting table between areas of code, no luck still have not been able prepare this, have many other projects working on , not @ coding.

<!doctype html> <html> <head> <script> function doevens() { var number = document.getelementbyid('num').value; if(number > 1) { if(number % 2 == 0) { while(number > 2) { document.getelementbyid("space").innerhtml += (+number - 2) + "; "; number = +number - 2; } } else { number--; alert(number); while(number > 1) { document.getelementbyid("space").innerhtml += (+number - 2) + "; "; number = +number - 2; } } } } </script> </head> <body> type in number: <input type="number" id="num"> <button onclick="doevens();" href="javascript;">submit</button> <p id="space"></p> </p> </body> </html>

i have implemented solution table. here is:

class="snippet-code-js lang-js prettyprint-override">function doevens() { var number = document.getelementbyid('num').value, table = document.getelementbyid('evenstable'), cellsinrow = 20, evens = [], tablecontent = '', row = '', = 3, startat, numberofrows; table.innerhtml = ''; while (i++ < number - 1) { % 2 === 0 && evens.push(i); } numberofrows = evens.length / 20; (var r = 0; r < numberofrows; r++) { row = '<tr>'; startat = r * cellsinrow; (var c = startat; c < startat + cellsinrow; c++) { if (!evens[c]) break; row += '<td>' + evens[c] + '</td>'; } row += '</tr>'; tablecontent += row; } table.innerhtml = tablecontent; } class="snippet-code-html lang-html prettyprint-override"><body> type in number: <input type="number" id="num"> <button onclick="doevens();" href="javascript;">submit</button> <table id="evenstable"> </table> </body>

this table contains 1 row, can create more rows in code (say after every 10 numbers print)

if not clear you, please inquire me.

update

i have updated solution per needs.

javascript function loops datatables

No comments:

Post a Comment