3 Replies - 389 Views - Last Post: 28 January 2012 - 07:12 PM

Topic Sponsor:

#1 mlantzm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-January 12

'for' loop program in a table

Posted 26 January 2012 - 03:39 PM

My assignment is to create an HTML file that has a Javascript program embedded in it. The Javascript should have a 'for' loop to create a table showing the numbers from 5 to 15 and their squares. I have figured out how to show the numbers and their squares, but I'm struggling with how to get in a table. This is what I have so far...
<body>
    <!-- File: unit4.htm -->
    <script type="text/javascript">
	
     for (var intNum=5 ; intNum<16 ; intNum++) 
	{  
          intSqr = intNum*intNum;
	  document.write (intNum);           
          document.write(" " + intSqr + " </br>");
	}

    </script>
</body>


I have tried adding the <table></table> tags with the <tr></tr> and <td></td> tags before the 'for' loop and in the 'for' loop, but it either displays nothing or doesn't change. I'm confused and not sure what route to take. If someone could give me an idea on how to place the table within my code, I'd appreciate it.

Mod edit - Please :code:

Is This A Good Question/Topic? 0
  • +

Replies To: 'for' loop program in a table

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 772
  • View blog
  • Posts: 6,133
  • Joined: 07-September 06

Re: 'for' loop program in a table

Posted 26 January 2012 - 03:44 PM

Basically, in Javascript HTML should be stored as a string. Attempting to put HTML directly into a script tag will cause problems (the JS parser won't be able to understand what is going on).

Here is a program that will write a 1x1 table and put My Table! inside of it:
<script>
  document.write("<table><tr><td>My Table!</td></tr></table>");
</script>


This is just a very basic example, but with it you should be able to figure out how to output HTML from Javascript.

Hope that helps.
Was This Post Helpful? 1
  • +
  • -

#3 SittingonDucks  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-December 11

Re: 'for' loop program in a table

Posted 28 January 2012 - 06:26 PM

Your simple question got me started on an entire experimental project. Take this if you want it:

Mod edit - Homework solution code removed.
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 772
  • View blog
  • Posts: 6,133
  • Joined: 07-September 06

Re: 'for' loop program in a table

Posted 28 January 2012 - 07:12 PM

@SittingonDucks - Please don't do people's homework for them. That isn't what this forum is about, and it is actually against the rules. I have removed the code from your previous post.

Here is the wording of the rule for future note:

Quote

Providing Help
As stated above, we do not do peoples homework for them. We are here to help. So please feel free to give back to the community by replying to questions. If a member does not include their code or is simply asking you to do their homework for them, you can type: [ rules ][ /rules ] (no spaces) to include our homework and code policy. You can also request a member paste their code in the appropriate [code] tags by using our : code : emoticon (again, no spaces).

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1