Pyramid Patterns – JavaScript – Part 1

Pattern 1:

<script type="text/javascript">
	for(i=1; i<=5; i++)
	{
		for(j=1; j<=i; j++)
		{
			document.write(" * ");
		}
		document.write("<br>");
	}
</script>

Pattern 2:

<script type="text/javascript">
	for(i=1; i<=5; i++)
	{
		for(j=1; j<=i; j++)
		{
			document.write(i);
		}
		document.write("<br>");
	}
</script>

Pattern 3:

<script type="text/javascript">
	for(i=1; i<=5; i++)
	{
		for(j=1; j<=i; j++)
		{
			document.write(j);
		}
		document.write("<br>");
	}
</script>

Pattern 4:

<script type="text/javascript">
	for(i=1; i<=5; i++)
	{
		for(j=1; j<=i; j++)
		{
			document.write(i+j);
		}
		document.write("<br>");
	}
</script>

Pattern 5:

<script type="text/javascript">
	for(i=1; i<=5; i++)
	{
		for(j=1; j<=i; j++)
		{
			document.write(j+i);
		}
		document.write("<br>");
	}
</script>

Note
We can use the <table> tag between JavaScript code to align the items in a proper way.

Pattern 6:

<script type="text/javascript">
	document.write("<table>");
	for(i=1; i<=5; i++)
	{
		document.write("<tr>");
		for(j=1; j<=i; j++)
		{
			k = i*j;
			document.write("<td>"+k+"</td>");
		}
		document.write("</tr>");
	}
	document.write("</table>");
</script>

Pattern 7:

<script type="text/javascript">
	document.write("<table>");
	for(i=1; i<=5; i++)
	{
		document.write("<tr>");
		for(j=1; j<=i; j++)
		{
			k = j*j;
			document.write("<td>"+k+"</td>");
		}
		document.write("</tr>");
	}
	document.write("</table>");
</script>

Pattern 8:

<script type="text/javascript">
	document.write("<table>");
	for(i=1; i<=5; i++)
	{
		document.write("<tr>");
		for(j=1; j<=i; j++)
		{
			k = i*i;
			document.write("<td>"+k+"</td>");
		}
		document.write("</tr>");
	}
	document.write("</table>");
</script>

Pattern 9:

<script type="text/javascript">
	document.write("<table>");
	for(i=1; i<=5; i++)
	{
		document.write("<tr>");
		for(j=1; j<=i; j++)
		{
			k = j-i;
			document.write("<td>"+k+"</td>");
		}
		document.write("</tr>");
	}
	document.write("</table>");
</script>

Pattern 10:

<script type="text/javascript">
	document.write("<table>");
	for(i=1; i<=5; i++)
	{
		document.write("<tr>");
		for(j=1; j<=i; j++)
		{
			k = 1-j-i;
			document.write("<td>"+k+"</td>");
		}
		document.write("</tr>");
	}
	document.write("</table>");
</script>

Pattern 11:

<script type="text/javascript">
	document.write("<table>");
	for(i=1; i<=5; i++)
	{
		document.write("<tr>");
		for(j=1; j<=i; j++)
		{
			if(j % 2 == 1)
			{
				document.write("<td>"+i+"</td>");
			}
			else
			{
				document.write("<td>"+"*"+"</td>");
			}
		}
		document.write("</tr>");
	}
	document.write("</table>");
</script>

Pattern 12:

<script type="text/javascript">
	document.write("<table>");
	for(i=1; i<=5; i++)
	{
		document.write("<tr>");
		for(j=1; j<=i; j++)
		{
			if(j % 2 == 0)
			{
				document.write("<td>"+j+"</td>");
			}
			else
			{
				document.write("<td>"+i+"</td>");
			}
		}
		document.write("</tr>");
	}
	document.write("</table>");
</script>

Pattern 13:

<script type="text/javascript">
	document.write("<table>");
	for(i=1; i<=5; i++)
	{
		document.write("<tr>");
		for(j=1; j<=i; j++)
		{
			if(i % 2 == 0)
			{
				document.write("<td>"+1+"</td>");
			}
			else
			{
				document.write("<td>"+0+"</td>");
			}
		}
		document.write("</tr>");
	}
	document.write("</table>");
</script>

Pattern 14:

<script type="text/javascript">
	document.write("<table>");
	for(i=1; i<=5; i++)
	{
		document.write("<tr>");
		for(j=1; j<=i; j++)
		{
			if(j % 2 == 0)
			{
				document.write("<td>"+1+"</td>");
			}
			else
			{
				document.write("<td>"+0+"</td>");
			}
		}
		document.write("</tr>");
	}
	document.write("</table>");
</script>

Pattern 15:

<script type="text/javascript">
	document.write("<table>");
	for(i=1; i<=5; i++)
	{
		document.write("<tr>");
		for(j=1; j<=i; j++)
		{
			if(i*j % 2 == 0)
			{
				document.write("<td>"+1+"</td>");
			}
			else
			{
				document.write("<td>"+0+"</td>");
			}
		}
		document.write("</tr>");
	}
	document.write("</table>");
</script>

Pattern 16:

<script type="text/javascript">
	document.write("<table>");
	for(i=1; i<=5; i++)
	{
		document.write("<tr>");
		for(j=1; j<=i; j++)
		{
			if(j % 2 == 0)
			{
				if(i%2==0)
				{
					document.write("<td>"+"*"+"</td>");
				}
				else
				{
					document.write("<td>"+1+"</td>");
				}
			}
			else
			{
				document.write("<td>"+0+"</td>");
			}
		}
		document.write("</tr>");
	}
	document.write("</table>");
</script>

Pattern 17:

<script type="text/javascript">
	document.write("<table>");
	for(i=1; i<=5; i++)
	{
		document.write("<tr>");
		for(j=1; j<=i; j++)
		{
			if(j % 2 == 0)
			{
				if(i%2==0)
				{
					document.write("<td>"+"*"+"</td>");
				}
				else
				{
					document.write("<td>"+"!"+"</td>");
				}
			}
			else
			{
				document.write("<td>"+"*"+"</td>");
			}
		}
		document.write("</tr>");
	}
	document.write("</table>");
</script>

Pattern 18:

<script type="text/javascript">
	document.write("<table>");
	for(i=1; i<=5; i++)
	{
		document.write("<tr>");
		for(j=1; j<=i; j++)
		{
			if(j % 2 == 0)
			{
				if(i%2==1)
				{
					document.write("<td>"+"*"+"</td>");
				}
				else
				{
					document.write("<td>"+"!"+"</td>");
				}
			}
			else
			{
				document.write("<td>"+"*"+"</td>");
			}
		}
		document.write("</tr>");
	}
	document.write("</table>");
</script>

Pattern 19:

<script type="text/javascript">
	document.write("<table>");
	for(i=1; i<=5; i++)
	{
		document.write("<tr>");
		for(j=1; j<=i; j++)
		{
			if(i % 2 == 1)
			{
				if(j%2==0)
				{
					document.write("<td>"+"*"+"</td>");
				}
				else
				{
					document.write("<td>"+"!"+"</td>");
				}
			}
			else
			{
				document.write("<td>"+"*"+"</td>");
			}
		}
		document.write("</tr>");
	}
	document.write("</table>");
</script>

Pattern 20:

<script type="text/javascript">
	document.write("<table>");
	for(i=1; i<=5; i++)
	{
		document.write("<tr>");
		for(j=1; j<=i; j++)
		{
			if(i % 2 == 0)
			{
				if(j%2==0)
				{
					document.write("<td>"+"*"+"</td>");
				}
				else
				{
					document.write("<td>"+"!"+"</td>");
				}
			}
			else
			{
				document.write("<td>"+"*"+"</td>");
			}
		}
		document.write("</tr>");
	}
	document.write("</table>");
</script>

Pattern 21:

<script type="text/javascript">
	document.write("<table>");
	k=1;
	for(i=1; i<=4; i++)
	{
		document.write("<tr>");
		for(j=1; j<=i; j++)
		{
			document.write("<td>"+k+"</td>");
			k++;
		}
		document.write("</tr>");
	}
	document.write("</table>");
</script>

… to be continued to the next part (Part 2)

DEAR READERS,
If you know some more patterns of this type and want them to be published, comment them above, we will post them.

Leave a Reply

Your email address will not be published. Required fields are marked *