Programs in Javascript


Programs in javascrpt


 

Fibonacci Series JavaScript Program

<html>

<body>

<script type=”text/javascript”>

var a=0,b=1,c;

document.write(“Fibonacci”);

while (b<=10)

{

document.write(c);

document.write(“<br/>”);

c=a+b;

a=b;

b=c;

}

</script>

</body>

</html>

 

 

 

 

Copy Text JavaScript Program

 

<html>
<head>
<title>
Copy text</title>
</head>
<body>
<center>
<h2>Copy text from different field</h2>
<p>
<input type=”text” style=”color: #FF0080;background-color: #C9C299″ id=”field1″ value=”Good Morning”>
<input type=”text” style=”color: #FF0080;background-color: #C9C299″ id=”field2″>
<button style=”background-color: #E799A3″ onclick=”document.getElementById(‘field2’).value = document.getElementById(‘field1’).value”>Click to Copy Text
</p>
</center>
</body>
</html>

 

Form Validation JavaScript Program

 

 

<html>
<head>
<script type=”text/javascript”>
function sub()
{
if(document.getElementById(“t1”).value == “”)
alert(“Please enter your name”);
else if(document.getElementById(“t2”).value == “”)
alert(“Please enter a password”);
else if(document.getElementById(“t2”).value != document.getElementById(“t3”).value)
alert(“Please enter correct password”);
else if(document.getElementById(“t4”).value == “”)
alert(“Please enter your address”);
else
alert(“Form has been submitted”);
}
</script>
</head>
<body>
<form>
<p align=”center”>
User Name:<input type=”text” id=”t1″><br><br>
Password:<input type=”text” id=”t2″><br><br>
Confirm Password:<input type=”text” id=”t3″><br><br>
Address:<textarea rows=”2″ cols=”25″ id=”t4″></textarea><br><br>
<input type=”button” value=”Submit” onclick=”sub()”>
<input type=”reset” value=”Clear All”>
</p>
</form>
</body>
</html>

 

 

 

JavaScript Popup Window Program

<html>
<head>
<script type=”text/javaScript”>
function see()
{
var c= confirm(“Click OK to see Google Homepage or CANCEL to see Bing Homepage”);
if (c== true)
{
window.location=”http://www.google.co.in/”;
}
else
{
window.location=”http://www.bing.com/”;
}
}
</script>
</head>
<body>
<center>
<form>
<input type=”button” value=”Click to chose either Google or Bing” onclick=”see()”>
</form>
</center>
</body>
</html>

 

 

Palindrome JavaScript Program (for beginners)
This is a simple JavaScript palindrome program with example.

<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>

 

 

Check Odd/Even Numbers JavaScript Program (for beginners)
This is a simple JavaScript program to check odd or even numbers with example.

<html> 
<head>
<script type=”text/javascript”> 
var n = prompt(“Enter a number to find odd or even”, “Type your number here”);
n = parseInt(n);
if (isNaN(n))
{
alert(“Please Enter a Number”);
}
else if (n == 0)
{
alert(“The number is zero”);
}
else if (n%2)
{
alert(“The number is odd”);
}
else
{
alert(“The number is even”);
}
</script> 
</head>
<body>
</body> 
</html>

 

 

 
Simple Switch Case JavaScript Program (for beginners)
This is a simple switch case JavaScript example program for beginners..

<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>

 

 

 

 

 

 

 

Addition of two numbers

 

  <html>
  <body>
  <P> add2.html: adds 2 numbers
  <pre>
  &lt;script&gt;
  var num1, num2, sum
  num1 = prompt(“Enter first number”)
  num2 = prompt(“Enter second number”)
  sum = parseInt(num1) + parseInt(num2) // “+” means “add”
  alert(“Sum = ” + sum) // “+” means combine into a string
  &lt;/script&gt;
  </pre>
   
  <script>
  var num1, num2, sum
  num1 = prompt(“Enter first number”)
  num2 = prompt(“Enter second number”)
  sum = parseInt(num1) + parseInt(num2) // “+” means “add”
  alert(“Sum = ” + sum) // “+” means combine into a string
  </script>
   

 

 

Area of circle

 

  <html>
  <body>
  <P> area.html: computes area of circle until no input
  <pre>
  &lt;script&gt;
  var rad, area;
  rad = prompt(“Enter radius”)
  while (rad != null) {
  area = 3.14 * rad * rad
  alert(“radius = ” + rad + “, area = ” + area)
  document.write(“&lt;br&gt; radius = ” + rad + “, area = ” + area)
  rad = prompt(“Enter radius”)
  }
  &lt;/script&gt;
  </pre>
   
  <script>
  var rad, area;
  rad = prompt(“Enter radius”)
  while (rad != null) {
  area = 3.14 * rad * rad
  alert(“radius = ” + rad + “, area = ” + area)
  document.write(“<br> radius = ” + rad + “, area = ” + area)
  rad = prompt(“Enter radius”)
  }
  </script>
   

 

 

 

Find maximum numeric value

  <html>
  <body>
  <P> max.html: find maximum number until it sees 0
  <pre>
  &lt;script&gt;
  var max = 0
  var num
  num = prompt(“Enter new value, or 0 to end”)
  while (num != 0) {
  if (parseFloat(num) &gt; max)
  max = num
  num = prompt(“Enter new value, or 0 to end”)
  }
  document.write(“&lt;P&gt; Max = ” + max)
  &lt;/script&gt;
  </pre>
   
  <script>
  var max = 0
  var num
  num = prompt(“Enter new value, or 0 to end”)
  while (num != 0) {
  if (parseFloat(num) > max)
  max = num
  num = prompt(“Enter new value, or 0 to end”)
  }
  document.write(“<P> Max = ” + max)
  </script>
   

 

Sorting of numbers

 

Enter first number and click enter

Enter second number click enter and so on

In end click on click box and click ok

Result

 

 

Squaring of number using while loop

<html>

<body>

<P> while.html: print i,i**2 up to n

<pre>

&lt;script&gt;

    var n = prompt(“Enter number”)

    while (n != null) {

        i = 0

        while (i &lt;= n) {

            document.write(“&lt;br&gt;” + i + ” ” + i*i)

            i = i + 1

        }

        n = prompt(“Enter number”)

    }

&lt;/script&gt;

</pre>

 

<script>

    var n = prompt(“Enter number”)

    while (n != null) {

        i = 0

        while (i <= n) {

            document.write(“<br>” + i + ” ” + i*i)

            i = i + 1

        }

        n = prompt(“Enter number”)

    }

</script>

 

 

No. of lines print

<html><body><P> for.html: foxtrot for loop<pre>&lt;script&gt;    alert(“ready?”)    for (count = 1; count &lt;= 500; count++)            document.write(“&lt;br&gt;” + count + ”   I will not throw paper airplanes in class.”)&lt;/script&gt;</pre> <script>    alert(“ready?”)    for (count = 1; count <= 500; count++)            document.write(“<br>” + count + ”   I will not throw paper airplanes in class.”)</script>

 

 

In this above program” I will not throw paper airplanes in class.” Will run 500 times.

Result:

 

Click on ok