ava script tutorial - a new approach by Ken J Ward

Java Script Tutorial - Creating a new method with a function


Main Site Topics

Ken Ward's JavaScript Tutorial 1

In the previous tutorial we made a new method for the Array object in javascript because it hasn't got one. We used the "new" operator. Here we will create new methods for the Array object using the keyword function. In this case we have invented two new methods for the array: indexof, which finds the first occurrence of a string in an array, and lastindexof, which finds the last occurrence of a string in an array. Where the string occurs only once in the array, both give the same answer.

Both methods are case sensitive, and find whole words only.

The following example demonstrates finding the first and the last occurrence of a word in an array. This time, you can make your own array, if you like, separating the items by commas (don't put any spaces in though).

The Example

Enter a list separated with commas (No spaces!):

Enter a name:

Find first occurrence

Find last occurrence:

Keeping the values the same as the demonstration above, clicking on the "First Occurrence" button gives an alert, "Present at 1", meaning the word "Dick" is the second item.

The code is:

// the function takes two parameters: the value of the text box containing the string with words separated by commas, and the one containing the single name.

function findFirst(Indx,name)

{

var myText=Indx.value;

var myWord=name.value;

//the word and the text are the arguments of this function

//we make a regular array using split(",") which makes its items from those separated by commas

myArray=myText.split(",");

//if the array contains a position greater than -1 (the items start at 0), then we have found a match

if (myArray.indexof(myWord)>-1)

{

alert('Present at:'+myArray.indexof(myWord))

}

else

alert('Absent');

}

So we use indexof to find the position of an item in an array. This finds the first occurrence.

The next code shows the function to find the last occurrence of an item in an array (should their be more than one, otherwise it finds the only occurrence and gives the same answer as the previous).

function findLast(Indx,name)

{

var myText=Indx.value;

var myWord=name.value;

myArray=myText.split(",");

if (myArray.lastindexof(myWord)>-1)

{

alert('Present at:'+myArray.lastindexof(myWord))

}

else

alert('Absent');

}

As before, we use a custom made method, lastindexof, to find the last occurrence of a string in an array.

Creating the Methods

As in the previous lesson, we create the new methods, but this time use the function object to create them rather than the new constructor.

Array.prototype.indexof = function(myItem) {

//we make a variable, result, equal to minus one, so if nothing changes it, we will get -1 from this function

var result=-1;

//check whether the array has any items or the function has the wrong number of arguments

if ((this.length<1)||(arguments.length!=1))

{

return -1;

exit;

}

//having checked whether we have any items in the array and whether we have the right number of arguments, we can procede.

for (i=0;i<this.length;i++) {

//check each item beginning at the beginning

if (this[i]==myItem) {

// We could do various things here, such as using the indexOf and lastIndexOf (methods for a string) to find out if the string was contained in the item, and convert to the same case to be case insentive... here we used equality

result=i;

break;

}//end of if statement

}//end of for statement

return result;

//Attach custom method to array object

if (!Array.prototype.indexof)

{

Array.prototype.indexof=indexof;

}

}

The final bit of code of relevance here is creating the lastindexof method for the array, which differs from the indexof method only in the for statement:

Array.prototype.lastindexof = function(myItem) {

result=-1;

if ((this.length<1)||(arguments.length!=1))

{

return -1;

exit;

}

for (i=this.length-1;i>-1;i--) {

//in the above, we start at the end and check the items towards the beginning. The rest of the code is the same as before

if (this[i]==myItem) {

result=i;

break;

}//end of if statement

}//end of for statement

return result;

//Attach custom method to array object

if (!Array.prototype.lastindexof)

{

Array.prototype.lastindexof=lastindexof;

}

}

 

 

Java Script Tutorial  TOC

 


[If you need to learn more about HTML visit the HTML Tutorial]

The original tutorial in JavaScript, JavaScript1

I am always pleased to hear from you.
Send your comments to