Printing Number Series using Loop – JavaScript

Series 1 – Printing “Hello!”:

<script type="text/javascript">
	for(i=1; i<=10; i++)
	{
		document.write("Hello!");
		document.write("<br>"); // to break the line
	}
</script>

Series 2 – Printing “Hello” with number:

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

Series 3 – Simple count from 1 o 10:

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

Series 4 – Table of 2 (Short):

<script type="text/javascript">
	for(i=1; i<=10; i++)
	{
		temp=i*2;
		document.write(temp+", ");
	}
</script>

Series 5 – Table of 3 (Short):

<script type="text/javascript">
	for(i=1; i<=10; i++)
	{
		temp=i*3;
		document.write(temp+", ");
	}
</script>

Series 6 – Square root up to 10:

<script type="text/javascript">
	for(i=1; i<=10; i++)
	{
		temp=i*i;
		document.write(temp+", ");
	}
</script>

Series 7 – Cube root up to 10:

<script type="text/javascript">
	for(i=1; i<=10; i++)
	{
		temp=i*i*i;
		document.write(temp+", ");
	}
</script>

Series 8 – Odd and Even Numbers:

<script type="text/javascript">
	for(i=1; i<=10; i++)
	{
		if(i%2==0)
		{
			document.write(i+" = Even Number"+"<br>");
		}
		else
		{
			document.write(i+" = Odd Number"+"<br>");
		}
	}
</script>

Series 9 – Divisibility check by 3:

<script type="text/javascript">
	for(i=1; i<=10; i++)
	{
		if(i%3==0)
		{
			document.write(i+" (Divisible by 3)"+"<br>");
		}
		else
		{
			document.write(i+"<br>");
		}
	}
</script>

Series 10 – Table of 2:

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

Series 11 – Table of 3:

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

Series 12 – Difference of series:

<script type="text/javascript">
	var b = 1;
	var f = 1;
	for(i=0; i<=10; i++)
	{
		f = i + b;
                a = b;
                b = f;
		document.write(f+", ");
	}
</script>

Series 13 – Difference of series:

<script type="text/javascript">
	temp = 0;
	for(i=0; i<=10; i++)
	{
		temp = temp + i;
		document.write(temp+", ");
	}
</script>

Series 14 – Fibonacci Series (With prompt box):

Enter the length of the Fibonacci Series, e.g.:10
Result up to the entered length
<script type="text/javascript">
	var length = parseInt(prompt("Enter Length"));
	var num1 = 0;
	var num2 = 1;
	for(i=1; i<=length; ++i)
	{
		document.write(num1+", ");
	 	sumOfPrevTwo = num1 + num2;
                num1 = num2;
                num2 = sumOfPrevTwo;
	}
</script>

Series 15 – Table of any number:

Enter any number of which you want the table to be printed. e.g.: 7
Table of the entered number

You can also set the length of the table with the prompt box. If you do so, two prompt boxes will appear, one is for the number other is for the length.

NOTE 1
If you don’t want to fix the length, start value or end value, you can use the prompt box (in any program).

NOTE 2
‘parseInt’, while using the prompt box, is used for the integer values only. If you want to put any string in the prompt box, use only prompt("Your Message");

NOTE 3
If you code for n number of prompt boxes, according to the requirement, the boxes will appear n times on the page.

DEAR READERS,
We would like to receive your valuable suggestions and some more pattern of series.

Leave a Reply

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