Sunday, January 28, 2007

array functions tutorial part II



Third function is called searchCount, simply beacuse it counts number of wor in array. It goes something like this:

function searchCount(wor, arr){
   var count = 0;
   for(i=0; i< arr.length; i=i+1){
     if(arr[i ]==wor){
       count += 1;
     }
   }
   return count;
}

Another function is union. If you have two arrays, union will return new array which consist of word that exists in both arrays.

function union(arr1, arr2){   var arr3 = new Array();
  count = 0;
  for(i=0; i< arr1.length; i=i+1){
    for(j=0; j< arr2.length; j=j+1){
      if(arr1[i ]== arr2[ j]){
        arr3[count ] = arr1[ i];
        count+=1;
      }
    }
  }
  return arr3;
}

One more important thing. Data type in these functions dont have to be String, it can be any type. For more strict evaluation, you can put === instead of ==.

- end of part II -
Posted by flanture at 17:29:28 | Permanent Link | Comments (0) |
Comments
Write a comment