Writing prime numbers to an html table

Page 1 of 1

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

#1 D.Mulroy

• D.I.C Regular

Reputation: 81
• 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

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }