JavaScript Sample Program Code for Beginners
1) Write a program using array to take n elements as input and display the elements of the array in JavaScript.
<html>
<head>
<script>
function arrayFunction()
{
var n = prompt("Enter a number of Elements: "); // to take input
var arr = [];
document.write("Elements of the Array : ");
for(i=0;i<n;i++)
{
arr[i] = prompt("Enter a number");
document.write(arr[i] + " , ");//print the elements
}
}
</script>
</head>
<body>
<input type="button" value="Click Me" onclick="arrayFunction()"> <!-- calling arrayFunction when u click the button--!>
</body>
<html>
2) Write a program to take 3 Numbers as input and Find the Maximum Number using JavaScript.
<html>
<head>
<script>
function maxNum(num1, num2, num3)
{
var num1 = prompt("Enter Number 1 : ");
var num2 = prompt("Enter Number 2 : ");
var num3 = prompt("Enter Number 3 : ");
var max = num1;// let us assume num1 is max
if(num2 >= max)
{
max = num2;
}
if(num3 >=max )
{
max=num3;
}
document.write("Max value = " + max);
}
</script>
</head>
<body>
<input type="button" value="Find Max" onclick="maxNum()"> <!-- calling arrayFunction when u click the button--!>
</body>
<html>
3) Write a program using array to take n Numbers as input and Find the Maximum Number using JavaScript.
<HTML>
<HEAD>
<TITLE>MaxJavaScriptPrograms</TITLE>
</HEAD>
<BODY>
<SCRIPT>
var i,n;
var max=0;
n=prompt("Enter Array size: ");
var a=new Array(n);//declaring array with n elements
for(i=0;i<n;i++)
{
a[i]=prompt("Enter numbers : ");
document.write(a[i] + " , ");
}
// finding Maximum……..
max=a[0]; // let us assume 1st element is max
for(i=1;i<n;i++)
{
if(max<a[i])
max=a[i];
}
document.write("<br>"+"Maximum = "+max);
</SCRIPT>
</BODY>
</HTML>
4) Write a program using array to take n Numbers as input and Find the Minimum Number using JavaScript.
<HTML>
<BODY>
<SCRIPT>
var i,n;
var min=0;
n=prompt("Enter Array Size= "); //input
var a=new Array(n);
for(i=0;i<n;i++)
{
a[i]=prompt("Enter numbers= ");// input
document.write(a[i]+" , ");
}
// finding Minimum……..
min=a[0]; //let us assume 1st element as min
for(i=1;i<n;i++)
{
if(min>a[i])
min=a[i];
}
document.write("Minimum Number= "+min);
</SCRIPT>
</BODY>
</HTML>
5) Write a program using array to take n Numbers as input and Find the sum of n numbers in an array using JavaScript.
<HTML>
<BODY>
<SCRIPT>
var i,n;
var sum=0;
n=prompt("Enter Array Size= ");
var a=new Array(n);
for(i=0;i<n;i++)
{
a[i]=prompt("Enter numbers= ");
document.write(a[i]+",");
}
// finding Sum of Array Numbers……..
sum=0;
for(i=0;i<n;i++)
{
sum= +sum + +a[i]; // we should use +before variable ..bcoz JavaScript uses + for string concatenation
//another procedure
//sum= sum+ parseInt(a[i]);
}
document.write("Sum of Array Numbers= "+sum);
</SCRIPT>
</BODY>
<HTML>
6) Write a program using array to take n Numbers as input and Find the average of n numbers in an array using JavaScript.
<HTML>
<BODY>
<SCRIPT>
var i,n;
var avg=0;
n=parseInt(prompt("Enter array size"));
var a=new Array(n);
for(i=0;i<n;i++)
{
a[i]=parseInt(prompt("Enter numbers"));
}
var sum=0;
for(i=0;i<n;i++)
{
sum= sum+ a[i];
}
// finding Average……..
avg=sum/n;
document.write("Array Element: ");
for(i=0;i<n;i++)
{
document.write(a[i]+ " , ");
}
document.write("Average= "+avg);
</SCRIPT>
</BODY>
</HTML>
7)Write a JavaScript code to calculate maximum, minimum, sum and average of numbers in an array.
<HTML>
<BODY>
<SCRIPT>
var i,n;
var max=0;
var min=0;
var sum=0;
var avg=0;
n=parseInt(prompt("Enter Array size : "));
var a=new Array(n);
for(i=0;i<n;i++)
{
a[i]=parseInt(prompt("Enter numbers : "));
}
// finding Maximum……..
max=a[0];
for(i=1;i<n;i++)
{
if(max<a[i])
max=a[i];
}
// finding Minimum……..
min=a[0];
for(i=1;i<n;i++)
{
if(min>a[i])
min=a[i];
}
// finding Sum of Array Numbers……..
sum=0;
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
// finding Average……..
avg=sum/n;
document.write("Array Elements :");
for(i=0;i<n;i++)
{
document.write(a[i]+ " " );
}
document.write("<br>"+"Maximumr= "+max+"<br>");
document.write("Minimum Number= "+min+"<br>");
document.write("Sum of Array Numbers= "+sum+"<br>");
document.write("Average= "+avg+"<br>");
</SCRIPT>
</BODY>
</HTML>
8)Write a JavaScript Program to compute all Armstrong numbers in the range of 0 and 1000.
Write a JavaScript Program to print sum of the digit of a number.
This program computes all Armstrong numbers in the range of
0 and 999. An Armstrong number is a number such that the Sum of its digits raised to the third power is equal to the number itself.
For example, 371 is an Armstrong number,
since 3^3 + 7^3 + 1^3 = 371.
Answer:
<HTML>
<HEAD>
<TITLE>MenuDriven</TITLE>
</HEAD>
<BODY>
<SCRIPT>
var i,j,r,ch;
var n;
var sum=0;
ch = parseInt(prompt("Enter 1 : Armstrong number & 2: sum of digits"));
switch(ch)
{
case 1:
document.write("Armstrong Number :- ");
for(i=1;i<=1000;i++)
{
j=i;
sum=0;
while(j>0)
{
r=j%10;
sum=sum+(r*r*r);
j=parseInt(j/10);
}
if( sum==i)
{
document.write(sum+" , ");
}
}
break;
case 2:
n=parseInt(prompt("Enter any number for finding sum of digits"));
while(n>0)
{
sum=sum+n%10;
n=parseInt(n/10);
}
document.write("<br>"+"Sumgits= "+sum);
break;
default :
alert("Wrong choice");
}
</SCRIPT>
</BODY>
<HTML>
9)Write a JavaScript Program to find Fibonacci Series up to n .
<html>
<body>
<script>
var a=0,b=1,c,n;
n=prompt("Enter the end Number of the Series = ");
document.write(" 0 , 1 , ");
while (b<=n)
{
c=a+b;
document.write(c);
document.write(" , ");
a=b;
b=c;
}
</script>
</body>
</html>
9)Write a JavaScript Program to check Palindrome Number.
<html>
<body>
<script type="text/javascript">
function Palindrome() {
var revStr = "";
var str = document.getElementById("str").value;
var i = str.length;
for(var j=i; j>=0; j--) {
revStr = revStr+str.charAt(j);
}
if(str == revStr) {
alert(str+" is Palindrome");
} else {
alert(str+" is not a Palindrome");
}
}
</script>
<form >
Enter a String/Number: <input type="text" id="str" name="string" /><br />
<input type="submit" value="Check" onclick="Palindrome();"/>
</form>
</body>
</html>
10)Write a JavaScript Program to sort numbers using insertion sort.
<html>
<body>
<script>
var item, i = 0, j, temp
var NumArray = new Array()
item = prompt("Enter new item, or OK to end")
while (item != "")
{
document.write("<br> " + item)
NumArray[NumArray.length] = item
item = prompt("Enter new item, or OK to end")
}
document.write("<P> " + NumArray.length + " items are sorted below")
// insertion sort
for (i = 0; i < NumArray.length-1; i++) {
for (j = i+1; j < NumArray.length; j++) {
if (NumArray[i] > NumArray[j]) {
temp = NumArray[i]
NumArray[i] = NumArray[j]
NumArray[j] = temp
}
}
}
// print NumArray
for (i = 0; i < NumArray.length; i++) {
document.write("<br> " + NumArray[i])
}
</script>
11) JavaScript Examples of switch-case-default-break to rpint the day.
<html>
<head>
<script type="text/javascript">
var n=prompt("Enter a number between 1 and 7");
switch (n)
{
case (n="1"):
document.write("Sunday");
break;
case (n="2"):
document.write("Monday");
break;
case (n="3"):
document.write("Tuesday");
break;
case (n="4"):
document.write("Wednesday");
break;
case (n="5"):
document.write("Thursday");
break;
case (n="6"):
document.write("Friday");
break;
case (n="7"):
document.write("Saturday");
break;
default:
document.write("Invalid Weekday");
break;
}
</script>
</head>
</html>
<html>
<body>
<script>
var n = prompt("Enter a number");
i = 0;
while (i <= n)
{
c=i*i;
i = i + 1;
}
document.write(c);
</script>
13) Write a JavaScript Program to the current Date and Time.
<script>
var todayDate=new Date();
var date=todayDate.getDate();
var month=todayDate.getMonth()+1;
var year=todayDate.getFullYear();
var hours=todayDate.getHours();
var minutes=todayDate.getMinutes();
var seconds=todayDate.getSeconds();
//document.writeln(todayDate);
document.writeln("Date = " + date + "/" + month + "/" + year);
document.writeln("Time =" + hours + " : " + minutes + " : " + seconds);
</script>
13) Write a JavaScript Program to the current Date and Time.
<script>
var todayDate=new Date();
var date=todayDate.getDate();
var month=todayDate.getMonth()+1;
var year=todayDate.getFullYear();
var hours=todayDate.getHours();
var minutes=todayDate.getMinutes();
var seconds=todayDate.getSeconds();
//document.writeln(todayDate);
document.writeln("Date = " + date + "/" + month + "/" + year);
document.writeln("Time =" + hours + " : " + minutes + " : " + seconds);
</script>