In Association with Amazon.in   Flipkart

Monday, September 30, 2013

JavaScript External Functions Linking To HTML Page

Filename: functions.js
====================================
// JavaScript Document

// Function With no Arguments and no return type
    function line1()  //  called function
    {
        document.write("<br/>**********################***********<br/>");
    }
   
    // Function With Arguments and no return type
    function line(s, n)  //  called function
    {
        document.write("<br/>");
        for(i=0; i<n; i++) {
            document.write(s);
        }
        document.write("<br/>");
    }

// Function With both Arguments and return type
    function clac(x, s, y)  //  called function
    {
        switch(s)
        {
        case '+': return(x+y); break;
        case '-': return(x-y); break;
        case '*': return(x*y); break;
        case '/': return(x/y); break;
        case '%': return(x%y); break;
           
        }
    }


===========================
File Name: Ext-jsfun-htmlpage.html

<head>
<script src="functions.js" type="text/javascript">
</script>
</head>
<body>
    <script>
    line1(); 
    res=clac(5, '*', 80);   //  calling function
    document.write("<br/>Res="+ res);
    line('$', 50);
    document.write("<br/>ANs=" + clac(52, '/', 3));
    </script>
 
</body>


Output:

**********################***********

Res=400
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

ANs=17.333333333333332


JavaScript Function With Both Arguments And Rreturn Type

<head>
<script>
// Function With both Arguments and return type
    function clac(x, s, y)  //  called function
    {
        switch(s)
        {
        case '+': return(x+y); break;
        case '-': return(x-y); break;
        case '*': return(x*y); break;
        case '/': return(x/y); break;
        case '%': return(x%y); break;
           
        }
    }
</script>
</head>
<body>
    <script>
    line1(); 
    res=clac(5, '*', 80);   //  calling function
    document.write("<br/>Res="+ res);
    line('$', 50);
    document.write("<br/>ANs=" + clac(52, '/', 3));
    </script>
</body>


output:

 Res=400
ANs=17.333333333333332


JavaScript Function With Arguments and no Return Type

<head>
<script>
// Function With Arguments and no return type
    function line(s, n)  //  called function
    {
        document.write("<br/>");
        for(i=0; i<n; i++) {
            document.write(s);
        }
        document.write("<br/>");
    }
</script>
</head>
<body>
    <script>
    line('*', 80);   //  calling function
    </script>
    <h2>Welcome to JS</h2>
    <script>
    line('$', 50);
    </script>
</body>

outpou:-



******************************************************

Welcome to JS


$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

JavaScript Function With no Arguments and no Return Type

<head>
<script>
// Function With no Arguments and no return type
    function line()  //  called function
    {
        document.write("<br/>**********################***********<br/>");
    }
</script>
</head>
<body>
    <script>
    line();   //  calling function
    </script>
    <h2>Welcome to JS</h2>
    <script>
    line();
    </script>
</body>


Output:


**********################***********

Welcome to JS


**********################***********

JavaScript Functions And Its Types

JavaScript Function: A function is used to group a set of statements then we can call no.of times any where in the project files. Functions are mainly used for re-usability.

Functions Are Mainly Two Types:
  1. Pre Defined/ Bulletin Functions 
  2. User Defined/ Derived Functions


Pre Defined/ Bulletin Functions :  Those functions which are defined by the system. We can use directly with out calling function definition.

Types of Pre Defined Functions: 
  • Number Function
  • Math Functions
  • String Functions
  • Date and Time Functions
  • Input and Output Functions
  • Array Functions
  • DOM Functions
 User Defined/ Derived Functions: Those functions which are defined by the User. We can use directly by calling function definition which is defined by user.

Types of User Defined Functions:
  • Function with no arguments and no return type
  • Function with arguments and no return type
  • Function with both arguments and with return type

Friday, September 27, 2013

JavaScript Multi-Dimensional Array With Example

Multi Dimensional Array:


Two/Multi-Dimensional Array is nothing but an Array in another Array

In an array each element is assigned with another array and so on....
i.e 2D Array, 3D Array, .....


Example:

<script>                  
var a = new Array(1234, "Kumar", "MGR", 35000);    // 1D Array               
var b = new Array(1534, "Ravi", "Acc", 25000);     // 1D Array  
var c = new Array(1643, "Hari", "HR", 15000);       // 1D Array                 
var emp = new Array(a, b, c);  //  2D Arry

document.write("<br/>emp Array length: " + emp.length);

//  To print All emp array elements
for(i=0; i<emp.length; i++)
{
    document.write("<br/>");
        for(j=0; j<emp[i].length; j++)
        document.write(emp[i][j] + "   ");
}

//  To print All emp array elements in the Table
document.write('<table align="center" border="5" width="400" ><tr><th>E.No</th><th>EName</th><th>Job</th><th>Sal</th></tr>');

for(i=0; i<emp.length; i++)
{
    document.write("<tr>");
        for(j=0; j<emp[i].length; j++) {
        document.write("<td>"+ emp[i][j] + "</td>");
        }
    document.write("</tr>");
}

document.write("</table>");
</script>


Output:


emp Array length: 3
1234 Kumar MGR 35000 
1534 Ravi Acc 25000 
1643 Hari HR 15000
E.NoENameJobSal
1234KumarMGR35000
1534RaviAcc25000
1643HariHR15000

Tuesday, September 10, 2013

JavaScript Associative Array WIth Example

JavaScript Associative Array: An array with Index/Keys as String. In Each array element we can store two values. In index one value and in array of index another value.


Example:
<script>

 a=new Array(4);
a['rno']=1234;
a['name']="Raju";
a['job']="Manager";
a['sal']=35000;

document.write(a['name']);          //  Raju


 // To Print All Associative Array Elements

 for(i in a) {
 document.write("<br/>" +  i + " =========> " + a[i]);
}

</script>

output:

Raju
rno =========> 1234
name =========> Raju
job =========> Manager
sal =========> 35000



JavaScript Numeric Array With Example

JavaScript Numeric Array: An array with index/keys as sequence numbers.

Syntax1: 
 a = new Array(123, "hello", 78.96);


Syntax2: 
 a = new Array();
a[0]=123;
a[1]="hello";
a[2]=78.96;

document.write(a); // 123, hello, 78.96
  
To find Array Length:  

document.write(a.length); // 3

To print all Array elements:

for(i=0; i<a.length; i++)
{
  document.write("<br/>" + a[i] );


output:-
123
hello
78.96
 

JavaScript Arrays And Its Types

Array: In JavaScript an Array is a collection of Heterogeneous Elements. So all type of values can be store in one array.

There are 3 types of Arrays in Javascript:

1. Numeric Array

2. Associative Array

3. Multi-Dimensional Array



Tuesday, September 3, 2013

JavaScript Alert Function With Example

Alert() :- To display alert message in the dialogue.



Example for alert

<html>
<head>
       
</head>
<body>
   <script>

           a = 10;        
           alert('Value of a is ' + a); 

    </script>


</body>
</html>

 

Related Posts Plugin for WordPress, Blogger...