Sunday, 2 February 2014

Arrays object

Arrays:
 It is a collection of elements of homogenious or different data types.

1. Creating Array
a) var n=[1,2,3,4,5];
var months[“Jan”, “Feb”, 3, 4, “May”];
b) var n=new array(1,2,3,4,5);
c) var n=new array[5];
2. Adding Elements : New element can be inserted at given position.
var n=[1,2,3,4,5];
var n[3]=6;
3. Accessing Elements
Last index is known by length attribute. So elements are accessed from beginning to
ending by using a loop.
Ex :
for(var i=0;i<ar.length;i++)
{
document.write(ar[i]);
}
Array indexes are zero-based, which means the first item is [0], second is [1], and so on.

Array Properties:
Here is a list of each property and their description.
Property
Description
Returns a reference to the array function that created the object.
index
The property represents the zero-based index of the match in the string
input
This property is only present in arrays created by regular expression matches.
Reflects the number of elements in an array.
The prototype property allows you to add properties and methods to an object.
Array Methods
Here is a list of each method and its description.
Method
Description
Returns a new array comprised of this array joined with other array(s) and/or value(s).
Returns true if every element in this array satisfies the provided testing function.
Creates a new array with all of the elements of this array for which the provided filtering function returns true.
Calls a function for each element in the array.
Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.
Joins all elements of an array into a string.
Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.
Creates a new array with the results of calling a provided function on every element in this array.
Removes the last element from an array and returns that element.
Adds one or more elements to the end of an array and returns the new length of the array.
Apply a function simultaneously against two values of the array (from left-to-right) as to reduce it to a single value.
Apply a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.
Reverses the order of the elements of an array -- the first becomes the last, and the last becomes the first.
Removes the first element from an array and returns that element.
Extracts a section of an array and returns a new array.
Returns true if at least one element in this array satisfies the provided testing function.
Represents the source code of an object
Sorts the elements of an array.
Adds and/or removes elements from an array.
Returns a string representing the array and its elements.
Adds one or more elements to the front of an array and returns the new length of the array.


<!DOCTYPE html>
<html>
<body>

<script>
var i;//creating variable i of type number
var c = new Array();//array creatin
//var c = new Array[];// invalid due to [] brackets
/* array initialisation*/
c[0] = "idli";
c[1] = "wada";
c[2] = "dosa";
/* or we can initialise
var c=new Array("idli","wada","dosa");
(or)
var c=["idli","wada","dosa"];
*/
for (i=0;i<c.length;i++)
{
document.write(c[i] + "<br>");
}
</script>
</body>
</html>
Examples
<html>
 <head>
  <title>accessing array elements</title>
 </head>
 <body>
  <script language="JavaScript">
 // f=new Array(5);//creating array of size 5
/*  f[0]="Rama";
  f[1]="Sita";
  f[2]="laxman";
  f[3]="Bharat";
  f[4]="Shathragna";*/
f=["Rama","Sita","laxman","Bharat","Shathragna",2,2.3,'m',true];//valid
//f={"Rama","Sita","laxman","Bharat","Shathragna"};//invalid
//var f=["Rama","Sita","laxman","Bharat","Shathragna"];//valid
//var f=("Rama","Sita","laxman","Bharat","Shathragna");//invalid
  //printing array elements
 var n=f.length;
for(var i=0;i<n;i++)
  document.write(f[i]+"</br>");


var j=f.join();//this method is used to print array elements in a single line
// j=f.join();//VALID
 document.write(j);
 </script>
</body>

</html>

<!-Program for Array http://improvejavascript.blogspot.in/-->
<html>
<head>
<script type="text/javascript">
var a=[1,2,3,4,5];
for(var i=0;i<a.length;i++)
document.writeln("<H1>"+a[i]+"</H1>");
</script>
</head>
<body>
</body>
</html>
<html>
 <head>
  <title>accessing array elements</title>
 </head>
 <body>
  <script language="JavaScript">
  var f=new Array("Rama","Sita","laxman","Bharat","Shathragna",2,2.3,'m',true);
//  var f=new array("Rama","Sita","laxman","Bharat","Shathragna",2,2.3,'m',true);////invalid due to small 'a'

  //printing array elements
  document.write(f[0]+"<br>");
  document.write(f[1]+"<br>");
  document.write(f[2]+"<br>");
  document.write(f[3]+"<br>");
  document.write(f[4]+"<br>");

  j=f.join();//this method is used to print array elements in a single line
  document.write(j);
 </script>
</body>
</html>

ExAMPLE:

<! javascript array automatically extends its size-->
<html>
 <head>
  <title>accessing array elements</title>
 </head>
 <body>
  <script language="JavaScript">
  var f=new Array(2);
//var f=new Array[2];//invalid due to [] brackes
f[0]="Rama";
  f[1]=1//semicolon is optional
  f[2]=3.2;


  //printing array elements
  document.write(f[0]+"<br>");
  document.write(f[1]+"<br>");
  document.write(f[2]+"<br>");
  document.write(f[3]+"<br>");
  document.write(f[4]+"<br>");

  j=f.join();//this method is used to print array elements in a single line
  document.write(j);
 </script>
</body>
</html>

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

<html>
 <head>
  <title>accessing array elements</title>
 </head>
 <body>
  <script language="JavaScript">
f=["Rama","Sita","laxman","Bharat","Shathragna",2,2.3,'m',true];//valid
// f=("Rama","Sita","laxman","Bharat","Shathragna");//invalid
//var f=["Rama","Sita","laxman","Bharat","Shathragna"];//valid
//var f=("Rama","Sita","laxman","Bharat","Shathragna");//invalid
  //printing array elements
  document.write(f[0]+"<br>");
  document.write(f[1]+"<br>");
  document.write(f[2]+"<br>");
  document.write(f[3]+"<br>");
  document.write(f[4]+"<br>");

  j=f.join();//this method is used to print array elements in a single line
  document.write(j);
 </script>
</body>
</html>
Example: search an element and remove it from the array
<! searching an array-->
<html>
 <head>
  <title>searching a element</title>
 </head>
 <body>
  <script language="JavaScript">

  var a=["Ram","kiran",34,36.54,"sita"];
//  a=["Ram","kiran",34,36.54,'sita',"sita"];//valid
  //display the array
  document.writeln("<p>");
  var n=a.length;
  for(var i=0;i<n;i++)
  {
   document.write(a[i]+ " ,");

  }
  document.writeln("</p>");
  //ask the user to remove item
  var r=prompt("Please type what you want to remove"," ");
  var temp=new Array(a.length-1);//we have to remove one element so -1 is must else we get "undefined"
  var m=0;
  
  //for loop searches, and removes
  for(var i=0;i<n;i++)
  {
   if(a[i]==r)
   {
    //does nothing
    ;
   }
   else
   {
    temp[m]=a[i];
    m++;
   }
  }//end of for loop
  //copying from array temp to array a
  a=temp;
  //display the new array
  document.write("<p>");
  n=a.length;
  for(var i=0;i<n;i++)
  {
   document.write(a[i]+" ,");
  }
  document.writeln("</p>");
//  document.close();
 </script>
 </body>
</html>

// same program using external program
function search()
{


  a=["Ram","kiran",34,36.54,'sita'];//valid
  //display the array
  document.writeln("<p>"); 
  var n=a.length;
  for(var i=0;i<n;i++)
  {
   document.write(a[i]+ " ,");

  }
  document.writeln("</p>");
  //ask the user to remove item
  var r=prompt("Please type what you want to remove"," ");
  var temp=new Array(a.length-1);
  var m=0;
   
  //for loop searches, and removes
  for(var i=0;i<n;i++)
  {
   if(a[i]==r)
   {
    //does nothing
    ;
   }
   else
   {
    
    temp[m]=a[i];
    m++;
   }
//  document.writeln(r+ "Not found");
  }//end of for loop
  //copying from array temp to array a
  a=temp;
  //display the new array
  document.write("<p>");
  n=a.length;
  for(var i=0;i<n;i++)
  {
   document.write(a[i]+" ,");
  }
  document.writeln("</p>");
  //document.close();
}
//save the above program example SearchJs.js

<! searching an array-->
<html>
 <head>
  <title>searching a element</title>
  <script type="text/JavaScript" src="SearchJs.js">
  </script>
 </head>
 <body>
 <input type="button" onclick="search()" value="remove"/>
 </body>
</html>



Array Properties
rere
Property
Description
Constructor
Returns a reference to the array function that created the object

Regular Expression Object

Regular Expressions

·         A regular expression is an object that describes a pattern of characters.
·         When you search in a text, you can use a pattern to describe what you are searching for.
·         A simple pattern can be one single character.
·         A more complicated pattern can consist of more characters, and can be used for parsing, format checking, substitution and more.
·          These are used to check and validate string type data. Called as objects because it
provides methods. It is two types, Static or Dynamic.
Ex : var name;
var exprs=new RegExp(“^[A-Z|a-z]”);
exprs.match(name)  True or False.
It specify that ‘name’ can start with any alphabet.
The JavaScript RegExp class represents regular expressions, and both String and RegExpdefine methods that use regular expressions to perform powerful pattern-matching and search-and-replace functions on text.
Syntax:
A regular expression could be defined with the RegExp( ) constructor like this:
var pattern = new RegExp(pattern, attributes);

or simply

var pattern = /pattern/attributes;
Here is the description of the parameters:
  • pattern: A string that specifies the pattern of the regular expression or another regular expression.
  • attributes: An optional string containing any of the "g", "i", and "m" attributes that specify global, case-insensitive, and multiline matches, respectively.
Brackets:
Brackets ([]) have a special meaning when used in the context of regular expressions. They are used to find a range of characters.

<!DOCTYPE html>
<html>
<body>

<p id="demo">Click the button to do a case-insensitive search for "Sr engi" in a string.</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction()
{
var s = "Visit SR Eng";
var p = /SR Eng/i;
var result = s.match(p);
document.getElementById("demo").innerHTML=result;
}
</script>

</body>

<!-- http://improvejavascript.blogspot.in/-->
</html>

Ex2:
<!DOCTYPE html>
<html>
<body>

<p id="demo">Click the button to do a global search for "is" in a string.</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction()
{
var str = "Is this all there is?";
var patt1 = /is/g;
var result = str.match(patt1);
document.getElementById("demo").innerHTML=result;
}
</script>

</body>

<!-- http://improvejavascript.blogspot.in/ -->
</html>

Ex3:
<!DOCTYPE html>
<html>
<body>

<script>
var patt1=new RegExp("e");

document.write(patt1.exec("The best things in life are free"));
</script>

</body>

<!-- http://improvejavascript.blogspot.in/ -->
</html>

Ex4:
<!DOCTYPE html>
<html>
<body>

<script>
var p=new RegExp("e");

document.write(p.test("The best things in life are free"));
</script>

</body>

<!-- http://improvejavascript.blogspot.in/ -->
</html>

Saturday, 25 January 2014

string object

A string simply stores a series of characters like "John Doe".

A string can be any text inside quotes. You can use single or double quotes:

syntax:
var val = new String(string);

Property
Description
Returns a reference to the String function that created the object.
Returns the length of the string.
The prototype property allows you to add properties and methods to an object.
Returns the character at the specified index.
Returns a number indicating the Unicode value of the character at the given index.
Combines the text of two strings and returns a new string.
Returns the index within the calling String object of the first occurrence of the specified value, or -1 if not found.
Returns the index within the calling String object of the last occurrence of the specified value, or -1 if not found.
Returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.
Used to match a regular expression against a string.
Used to find a match between a regular expression and a string, and to replace the matched substring with a new substring.
Executes the search for a match between a regular expression and a specified string.
Extracts a section of a string and returns a new string.
Splits a String object into an array of strings by separating the string into substrings.
Returns the characters in a string beginning at the specified location through the specified number of characters.
Returns the characters in a string between two indexes into the string.
The characters within a string are converted to lower case while respecting the current locale.
The characters within a string are converted to upper case while respecting the current locale.
Returns the calling string value converted to lower case.
Returns a string representing the specified object.
Returns the calling string value converted to uppercase.
Returns the primitive value of the specified object.
<!DOCTYPE html>
<html>
<body>

<script>
var carname1="Volvo XC60";
var carname2='Volvo XC60';
var answer1="It's alright";
var answer2="He is called 'Ram'";
var answer3='He is called "Ravi"';

document.write(carname1 + "<br>")
document.write(carname2 + "<br>")
document.write(answer1 + "<br>")
document.write(answer2 + "<br>")
document.write(answer3 + "<br>")
</script>

</body>

<!--http://improvejavascript.blogspot.in/ -->
</html>
//match()
<!DOCTYPE html>
<html>
<body>

<script>

var str="Hello world!";
document.write(str.match("world") + "<br>");
document.write(str.match("World") + "<br>");
document.write(str.match("world!"));

</script>

</body>
<!output is:world
null
world!-->

</html>
//toUpperCase() method
<!DOCTYPE html>
<html>
<body>

<script>

var txt="Hello World!";
document.write("<p>" + txt.toUpperCase() + "</p>");
document.write("<p>" + txt.toLowerCase() + "</p>");
document.write("<p>" + txt + "</p>");

</script>

<p>
The methods returns a new string.
The original string is not changed.
</p>
</body>
</html>
//split() method

<!DOCTYPE html>
<html>
<body>

<p id="demo">Click the button to display the array values after the split.</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction()
{
var str="a,b,c,d,e,f";
var n=str.split(",");
document.getElementById("demo").innerHTML=n[0];
}
</script>


</body>


</html>
//Ex2 for split method
<!DOCTYPE html>
<html>
<body>

<p id="demo">Click the button to display the array values after the split.</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction()
{
var str="a,b,c,d,e,f";
var n=str.split(",");
document.getElementById("demo").innerHTML=n[0];
}
</script>


</body>


</html>

//Ex3 for String object
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>

<script>
var x = "Ram";              // x is a string
var y = new String("Ram");  // y is an object
var txt = typeof(x) + " " + typeof(y);
document.getElementById("demo").innerHTML=txt;
</script>

</body>


</html>