Javascript

 1.      Write a JavaScript program to add two numbers

<html>

            <head>

                        <title>Sum of Two Numbers</title>

            </head>

            <body>

                        <script>

                                    var a = parseInt(prompt("Enter 1st number"));

                                    var b = parseInt(prompt("Enter 2nd number"));

                                    var sum = a + b;

                                    document.write("The sum is: " + sum);

                        </script>

            </body>

</html>

 

Using function

<html>

            <head>

                        <title>Sum of Two Numbers</title>

            </head>

            <body>

                        <script>

                                    var a = parseInt(prompt("Enter 1st number"));

                                    var b = parseInt(prompt("Enter 2nd number"));

 

                                    function sum(a, b) {

                                                var x = a + b;

                                                document.write("The sum is: " + x);

                                    }

 

                                    sum(a, b);

                        </script>

            </body>

</html>

2.Write a JavaScript program to find simple interest.

<html>

            <head>

                        <title>Simple interest</title>

            </head>

            <body>

                        <script>

                                    var p = parseInt(prompt("Enter Principle"));

                                    var t = parseInt(prompt("Enter time"));

      var r = parseInt(prompt("Enter rate"));

                                    var si = (p*t*r)/100;

                                    document.write("Simple Interest=Rs " +si);

                        </script>

            </body>

</html>

Using Function

<html>

            <head>

                        <title>Area of circle</title>

            </head>

            <body>

                        <script>

                                    var p = parseInt(prompt("Enter principle"));

                                                var t = parseInt(prompt("Enter time"));

                                                var r = parseInt(prompt("Enter rate"));

                                function si(p,t,r)

                                                {

                                    var si = (p*t*r)/100;

                                        document.write("Simple interest=Rs" +si);

                                }

                                si(p,t,r);

                        </script>

            </body>

</html>

3. Write a JavaScript program to find area of circle.

<html>

            <head>

                        <title>area of circle</title>

            </head>

            <body>

                        <script>

                                    var r = parseInt(prompt("Enter radius"));

                                    var area = (22/7)*r*r;

                                    document.write("Area of circle=" +area);

                        </script>

            </body>

</html>

Using  function

  <html>

            <head>

                        <title>Area of circle</title>

            </head>

            <body>

                        <script>

                                    var r = parseInt(prompt("Enter radius"));

                                function area(r)

                                                {

                                    var area = (22/7)*(r*r);

                                        document.write("Area of circle: " + area);

                                }

                                area(r);

                        </script>

            </body>

</html>

   4.Write a JavaScript program to find greatest among of two numbers.

<html>

            <head>

                        <title>Greatest among of two numbers</title>

            </head>

            <body>

                        <script>

                                    var a = parseInt(prompt("Enter 1st number"));

                                    var b = parseInt(prompt("Enter 2nd number"));

                                                                       

                                    if(a>b)

                                    {

                                                alert(a+ "is greatest");

                                    }

                                   

                                    else

                                    {

                                                alert(b+ "is greatest");

                                    }

 

                        </script>

            </body>

</html>

 Using Function

<html>

            <head>

                        <title>Greatest among of two numbers</title>


            </head>


            <body>


                        <script>


                                    var a = parseInt(prompt("Enter 1st number"));


                                    var b = parseInt(prompt("Enter 2nd number"));


                                   function greatest(a,b)

{                                    


                                    if(a>b)


                                    {


                                                alert(a+ "is greatest");


                                    }


                                   


                                    else


                                    {


                                                alert(b+ "is greatest");


                                    }


 }

greatest(a,b);

                        </script>


            </body>


</html>

5.      Write a JavaScript program to find greatest among of three numbers.

<html>

            <head>

                        <title> Greatest among of three numbers</title>

            </head>

            <body>

                        <script>

                                    var a = parseInt(prompt("Enter 1st number"));

                                    var b = parseInt(prompt("Enter 2nd number"));

                                    var c = parseInt(prompt("Enter 3rd number"));

                                    if(a>b&&a>c)

                                    {

                                                alert(a+ "is greatest");

                                    }

                                    else if(b>a&&b>c)

                                    {

                                                alert(b+ "is greatest");

                                    }

                                    else

                                    {

                                                alert(c+ "is greatest");

                                    }

 

                        </script>

            </body>

</html>

Using Function
<html>

            <head>

                        <title> Greatest among of three numbers</title>

            </head>

            <body>

                        <script>

                                    var a = parseInt(prompt("Enter 1st number"));

                                    var b = parseInt(prompt("Enter 2nd number"));

                                    var c = parseInt(prompt("Enter 3rd number"));

    function greatest(a,b,c)  
   {                                  
if(a>b&&a>c)

                                    {

                                                alert(a+" is greatest");

                                    }

                                    else if(b>a&&b>c)

                                    {

                                                alert(b+" is greatest");

                                    }

                                    else

                                    {

                                                alert(c+" is greatest");

                                    }

  }
greatest(a,b,c);
                        </script>

            </body>

</html>

6.      Write a JavaScript program to check whether the number is positive, negative or zero

            <html>
<head>

                        <title>positive negative zero</title>

            </head>

            <body>

                        <script>

                                    var n = parseInt(prompt("Enter a number"));

                                   

                                    if(n>0)

                                    {

                                                document.write(n+" is positive");

                                    }

                                   

                                    else if(n<0)

                                    {

                                              document.write(n+" is negative");

                                    }
else 

                                    {

                                             document.write(n+" it is zero");

                                    }


                        </script>

            </body>

</html>
   
Using Function
<    <html>

            <head>

                        <title>positive negative zero</title>

            </head>

            <body>

                        <script>

                                    var n = parseInt(prompt("Enter a number"));

                                    function check(n)
{                                    

                                    if(n>0)

                                    {

                                                document.write(n+" is positive");

                                    }

                                   

                                    else if(n<0)

                                    {

                                              document.write(n+" is negative");

                                    }
else 

                                    {

                                             document.write(n+" it is zero");

                                    }
      check(n);


 }

                        </script>

            </body>

</html>
               

7.      Write a JavaScript program to check whether the number is odd or even. 
<html>

            <head>

                        <title>odd or even</title>

            </head>

            <body>

                        <script>

                                    var n = parseInt(prompt("Enter a number"));
r=n%2;
                                  

                                    if(r==0)

                                    {

                                                document.write(n+" is even");

                                    }

                                   

                                    
else 

                                    {

                                             document.write(n+"  is odd");

                                    }


                        </script>

            </body>

</html>

Using Function
<html>
<head>
    <title>Odd Even using Function</title>
</head>
<body>
<script>

    function check(n) 
    {
        var r = n % 2;   
        if (r == 0) 
        {
            document.write(n + " is even");
        } 
         else 
         {
            document.write(n + " is odd");
        }
    }
    var n = parseInt(prompt("Enter a number"));
    check(n);
</script>
</body>
</html>

     8. 1,2,3…………….upto 10th term

<html>

            <head>

                        <title> series</title>

            </head>

            <body>

                        <script>

                         for(i=1;i<=10;i=i+1)

                        {

                                    document.write(i  +"<br>");

                        }

                        </script>

            </body>

</html>

9. 1,3,5,……… upto 10th term

<html>

            <head>

                        <title> series</title>

            </head>

            <body>

                        <script>

                                   a=1;

                                     for(i=1;i<=10;i=i+1)

                        {

                                    document.write(a +" ");

                                    a=a+2;

                        }

                        </script>

            </body>

</html>

10.2,4,6…..upto 10th term

<html>

            <head>

                        <title> series</title>

            </head>

            <body>

                        <script>

                                   a=2;

                                     for(i=1;i<=10;i=i+1)

                        {

                                    document.write(a +" ");

                                    a=a+2;

                        }

                        </script>

            </body>

</html>

]12. 2 4 7 11 16 ……….upto 10th term

<html>

            <head>

                        <title> series</title>

            </head>

            <body>

                        <script>

                                   a=1;

                                     for(i=1;i<=10;i=i+1)

                        {

                                    document.write(a +" ");

                                    a=a+i;

                        }

                        </script>

            </body>

</html>

13. 5 10 15 20……………..upto 10th term

<html>

            <head>

                        <title> series</title>

            </head>

            <body>

                        <script>

                                  

                                     for(i=1;i<=10;i=i+1)

                        {

                                    a=5;

                                    a=a*i;

                                    document.write(a +" ");

                        }

                        </script>

            </body>

14. 5,25 125 625………..upto 10th term

<html>

            <head>

                        <title> series</title>

            </head>

            <body>

                        <script>

                                   a=5;

                                     for(i=1;i<=10;i=i+1)

                        {

                                    document.write(a +" ");

                                    a=a*5;

                                   

                        }

                        </script>

            </body>

</html>

15. Write a JavaScript Code snippet, along with HTML, that displays the multiplication table of a entered number.

<html>

<head>

    <title>Multiplication Table</title>

</head>

<body>

<script>

    var a = parseInt(prompt("Enter a number"));

 

    for (var i = 1; i <= 10; i++) {

        document.write(a + " × " + i + " = " + (a * i) + "<br>");

    }

</script>

</body>

</html>1

6. Write a JavaScript code to calculate the factorial of a given number.

<html>

<head>

    <title>Factorial of a Number</title>

</head>

<body>

<script>

    var n = parseInt(prompt("Enter a number"));

    var fact = 1;

    for (var i = 1; i <= n; i++) {

        fact = fact * i;

    }

    document.write("Factorial of " + n + " is: " + fact);

</script>

</body>

</html>


Write a program to display factors of a given number.

<html>

<head>

<title>Factor</title>

</head>

<body>

<script>

var n = parseInt(prompt("Enter a number"));

document.write("Factors: ");


for (var i = 1; i <= n; i++) {

    if (n % i === 0) {

        document.write(i + " ");

    }

}

</script>

</body>

</html>


onclick Event Type

This is the most frequently used event type which occurs when a user clicks the left button of his mouse. User can put validation, warning etc., against this event type.

Example:

<html> <head> <title>on click</title> </head> <script type="text/javascript"> <!-- function sayHello() { alert("Hello World") } //--> </script> <body> <p>Click the following button and see result</p> <form> <input type="button" onclick= "sayHello()" value="Say Hello"/> </form> </body> </html>









Form Validation
<!DOCTYPE html>
<html>
<head>
<title>Form Validation</title>

<script type="text/javascript">
function validate() {

    if (document.myForm.Name.value == "") {
        alert("Please provide your name!");
        document.myForm.Name.focus();
        return false;
    }

    if (document.myForm.EMail.value == "") {
        alert("Please provide your Email!");
        document.myForm.EMail.focus();
        return false;
    }

    if (document.myForm.Zip.value == "" ||
        isNaN(document.myForm.Zip.value) ||
        document.myForm.Zip.value.length != 5) {
        
        alert("Please provide a zip in the format #####");
        document.myForm.Zip.focus();
        return false;
    }

    if (document.myForm.Country.value == "-1") {
        alert("Please select your country!");
        return false;
    }

    return true;
}
</script>

</head>

<body>

<form action="/cgi-bin/test.cgi" name="myForm" onsubmit="return 

validate();">

<table cellspacing="2" cellpadding="2" border="1">

<tr>
<td align="right">Name:</td>
<td><input type="text" name="Name" /></td>
</tr>

<tr>
<td align="right">Email:</td>
<td><input type="text" name="EMail" /></td>
</tr>

<tr>
<td align="right">Zip Code:</td>
<td><input type="text" name="Zip" /></td>
</tr>

<tr>
<td align="right">Country:</td>
<td>
<select name="Country">
    <option value="-1" selected>[choose yours]</option>
    <option value="1">Nepal</option>
    <option value="2">China</option>
    <option value="3">Bangladesh</option>
</select>
</td>
</tr>

<tr>
<td></td>
<td><input type="submit" value="Submit" /></td>
</tr>

</table>

</form>

</body>
</html>

Popular posts from this blog

Computer

Sequential Programs