I was looking at a C tutorial on Drexel University’s site when I saw an example for generating a sine table. I decided to try adapting this little program to JavaScript. I ended up creating a rudimentary table object to render the data in a HTML table with a caption and header. The calculations use the Math.atan() and Math.sin() functions from JavaScript’s Math object.
The original version of the code printed formatted ASCII text to standard out. I decided I wanted to output the data in a table that was creating dynamically using JavaScript. The resulting htmlTable object allows you to set a caption, add a row, add a header row, set the width and enable table striping. There are many more properties and methods that could be added to the table object. The most obvious being the ability to add a table footer.
Here is my JavaScript table object:
1 // htmlTable object by Levi Senft 2 var htmlTable = 3 4 // Initialize basic html elements needed to create an empty table 5 6 7 // Create the table html element 8 thistable = documentcreateElement"table"; 9 10 // Create the tbody html element 11 thistableBody = documentcreateElement"tbody"; 12 13 // Create the thead html element 14 thistableHead = thistablecreateTHead; 15 16 17 18 // Be default we aren't going to table stripe 19 stripe: false 20 21 // Place holder to store our table element 22 table: null 23 24 // Place holder to store our table body element 25 tableBody: null 26 27 // Place holder to store our table head element 28 tableHead: null 29 30 // Function to add a row to the table's body 31 32 33 // Create a new table row 34 var newRow = documentcreateElement"tr"; 35 36 // loop through the array of data passed to this function 37 for var i = 0; i < cellArraylength; i++ 38 39 // Create and insert a new cell into the new table row 40 var newCell = documentcreateElement"td"; 41 42 // Insert data into the table cell 43 newCellinnerHTML = cellArrayi; 44 newRowappendChildnewCell; 45 46 47 // Insert the row at the bottom of the table 48 thistableBodyappendChildnewRow; 49 50 51 52 // Function to add a row to the table's body 53 54 55 // Create and insert a new table row into the table's thead element 56 var newRow = thistableHeadinsertRow0; 57 58 // Loop through the data passed as an array to the addHead function 59 for var i = 0; i < cellArraylength; i++ 60 61 // Create a new th element 62 var newCell = documentcreateElement"th"; 63 64 // Add the array data to the cell's inner html 65 newCellinnerHTML = cellArrayi; 66 67 // Append the cell to the row. 68 newRowappendChildnewCell; 69 70 71 72 73 74 // Set the table's caption 75 76 77 thistablecreateCaptioninnerHTML = caption; 78 79 80 81 // Set the table's width 82 83 thistablestylewidth = width; 84 85 86 87 // Render the table into an element 88 89 90 // append the dynamically generated tbody to the table element 91 thistableappendChildthistableBody; 92 93 // Did we want striping? 94 if thisstripe 95 96 // Loop through all the rows 97 for var i = 0; i < thistableBodyrowslength; i++ 98 99 // Is this an even row? 100 if i % 2 != 0 101 102 // It is! append a class name that will have an 103 104 // alternate background color 105 thistableBodyrowsiclassName = "even"; 106 107 108 109 110 111 // Append the table to the specified element 112 elementappendChildthistable; 113 114 115 116 ; 117
Here is the HTML page with an embedded script that generates the data for the sine table:
1 3 4 5 6 JavaScript Sine Table 7 8 54 55 56 57 58 107 108 109
Here is a link to the demo.
Very cool. Now we should have JavaScript graph it.