0 Replies - 1180 Views - Last Post: 26 January 2011 - 02:22 PM

#1 D.Mulroy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 81
  • View blog
  • Posts: 430
  • Joined: 30-June 10

Writing prime numbers to an html table

Posted 26 January 2011 - 02:22 PM

Description: Copy and paste the header script and call primeNumber(); in a body script.Write all prime numbers from 1-100 in a html table. Prime numbers will be in red text.
<style type="text/css">
.primes
{
	color: red;
}
</style>
<script type="text/javascript">
function primeNumbers()
{
	var tableCell = 0;
	document.write("<table cols='10' border='1'>");
	document.write("<tr>");
	document.write("<th  colspan='10' align='center'>Prime Numbers(1-100)</th>");
	document.write("</tr>");
	for(i=0;i <= 100;i++)
	{
		if(i == 1)
		{
			
			document.write("<td>"+ i + "</td>");
			tableCell++;	
		}
		else
		{
			for(c=2;c <= i - 1 || i == 2;c++)
			{
				if(i == 2)
				{
					document.write("<td class='primes'>" + i + "</td>");
					tableCell++;
					break;
				}
				if(i % c == 0)
				{
					if(tableCell == 9)
					{
						document.write("<td>" + i + "</td></tr>");
						tableCell = 0;
					}
					else if(tableCell == 0)
					{
						document.write("<tr><td>" + i + "</td>");
						tableCell++;	
					}
					else
					{
						document.write("<td>" + i + "</td>");
						tableCell++;
					}
					break;
				}
				else if(i % c != 0 && c == i - 1)
				{	
					if(tableCell == 9)
					{
						document.write("<td class='primes'>" + i + "</td></tr>");
						tableCell = 0;
					}
					else if(tableCell == 0)
					{
						document.write("<tr><td class='primes'>" + i + "</td>");
						tableCell++;	
					}
					else
					{
						document.write("<td class='primes'>" + i + "</td>");
						tableCell++;
					}
					break;	
				}
				else
				{
					continue;
				}
			}
		}
	}
	document.write("</table>");
}

</script>



Is This A Good Question/Topic? 0
  • +

Page 1 of 1