Results 1 to 5 of 5

Thread: JavaScript Help!

  1. #1
    Join Date
    Jun 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JavaScript Help!

    I have a challenging problem I am working on. The problem says: Write a script that calculates the squares and cubes of the numbers from 0 to 10 and outputs XHTML text that displays the resulting values in an XHTML table format.

    The code I have produced so far is here. I have been having trouble putting it in a table format. Any help is welcome!

    Code:
    <?xml version = "1.0" encoding = "utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns = "http://www.w3.org/1999/xhtml">
    	<head>
    		<title>Calculating Squares and Cubes</title>
    		<style type="text/CSS">
    			span {color: orange;}
    		</style>
    		<script type="text/javascript">
    			<!--
    			var square;
    			var cube;
    			document.write("<span>number\tsquare\tcube</span><br />");
    			for (var number = 0; number <=10; number +=1)
    			{
    				square = number*number;
    				cube = number*number*number;			
    				document.writeln(number);
    				document.writeln(square);
    				document.writeln(cube);
    				document.write("<br />");
    			}			
    			// -->
    		</script>
    		
    	</head>
    
    	<body>
    		
    	</body>
    </html>

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    We don't do homework assignments here. You wouldn't really be learning much if we did the work for you anyway.

    But we have some participants here who are just learning this stuff themselves. If one of them wants to try helping you, at least someone will be learning something.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    EDIT - Scrap what I said, what you're asking for is very simple, so I don't feel bad telling you how to do it (you've already got the basics down-pat)

    Code:
    <?xml version = "1.0" encoding = "utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns = "http://www.w3.org/1999/xhtml">
    <head>
    <title>Calculating Squares and Cubes</title>
    <style type="text/CSS">
    span {
    	color: orange;
    }
    </style>
    <script type="text/javascript">
    <!--
    var square;
    var cube;
    document.write("<table border='1'>");
    for (var number = 0; number <=10; number +=1){
    	square = number*number;
    	cube = number*number*number;	
    	document.write("<tr>");
    	document.write("<td>");
    	document.write(number);
    	document.write("</td>");
    	document.write("<td>");
    	document.write(square);
    	document.write("</td>");
    	document.write("<td>");
    	document.write(cube);
    	document.write("</td>");
    	document.write("</tr>");
    }	
    document.write("</table>");			
    // -->
    </script>
    </head>
    <body>
    </body>
    </html>
    That should do what you asked for... I made a few minor changes to what you already had too.
    If you have any questions, please ask! I want you to know how it works, not just that it does work!
    Keyboard1333
    Last edited by keyboard; 06-19-2012 at 12:08 PM.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Although it works, using document.write or document.writeln is rarely the ideal method for something like this. If you're interested in alternate methods, complete with speed trials, see:

    http://www.quirksmode.org/dom/innerhtml.html

    You can click on the various methods listed to see how long each one takes. Repeated clicks will create average times for that method. Clicking two or more methods will create relative rankings between the methods.

    The created table is displayed each time at the bottom of the page, but it's not much to look at. Any such table is removed before a new on is created.

    You can use your browser's 'view source' to see the various ways tables are being created for the tests.

    All these tables output are asterisks. But the code that makes any of them can be adapted to the math and layout used for the document.write approach.
    Last edited by jscheuer1; 06-20-2012 at 01:59 AM. Reason: English usage
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Apr 2012
    Location
    Chester, Cheshire
    Posts
    329
    Thanks
    7
    Thanked 35 Times in 35 Posts

    Default

    Here's my effort. I've annotated the code for ease of interpretation.

    HTML Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="pageHead">
        <!-- Page Title-->
        <title>Squares and Cubes</title>
        <!-- Meta Block -->
        <meta content="text/html; charset=iso-8859-1" http-equiv="content-type" />
        <meta content="Squares and Cubes" name="description" />
        <meta content="square, cube, numbers" name="keywords" />
        <meta content="all,index,follow" name="robots" />
        <meta content="noodp" name="msnbot" />
        <meta content="global" name="distribution" />
        <!-- JavaScript Scripts-->
        <script type="text/javascript">
            //<[CDATA[
    
            /*
            * Function: displayTable - Outputs XHTML values to the page.
            */
            function displayTable() {
    
                /*
                * Locate "tblBody" tbody element on the page.
                */
                var el = document.getElementById('tblBody');
    
                /*
                * Inject XHTML into the table, using Math.pow(n,p);
                */
                for (i = 0; i <= 10; i++) {
    
                    /*
                    * Create local variable, strXHTML, to house the XHTML to output.
                    */
                    var strXHTML = '';
    
                    /*
                    * Build each table row with calculated values.
                    */
                    strXHTML += '<tr>';
                    strXHTML += '   <td>' + i + '</td>';
                    strXHTML += '   <td>' + Math.pow(i, 2) + '</td>';
                    strXHTML += '   <td>' + Math.pow(i, 3) + '</td>';
                    strXHTML += '</tr>';
    
                    /*
                    * Inject temporary XHTML into the table body.
                    */
                    el.innerHTML += strXHTML;
                }
            }
    
            /*
            * When the page loads, run the above code.
            */
            window.addEventListener('load', displayTable, false);
            //]]>
        </script>
        <!-- CSS Styles -->
        <style type="text/css">
            /*
            * TABLE STYLES
            */
            table, td, th
            {
                border: 1px solid #000000;
                border-collapse: collapse;
                padding: 5px;
                margin: 5px;
                text-align: center;
            }
        </style>
    </head>
    <body>
        <div id="page">
            <table>
                <tbody id="tblBody">
                    <tr>
                        <th>
                            n
                        </th>
                        <th>
                            n*n
                        </th>
                        <th>
                            n*n*n
                        </th>
                    </tr>
                </tbody>
            </table>
        </div>
    </body>
    </html>
    Last edited by ApacheTech; 06-19-2012 at 05:10 PM. Reason: Fixed some spelling errors in the annotations. Grr!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •