31 January 2015

Search Array JavaScript functions port

Eons ago there existed Search Array ActionScript functions post about completely non-efficient code, though mentioned on few places in educational purposes only. That was my starting point for doing same thing again, this time in JavaScript, but with few modifications.

Languages are very similar, there was just few extra ;s and some deleting of type declarations, that's all.

app.js

// JavaScript search array functions port from AS3
// @author: http://flanture.blogspot.com Jan 2015

var a = [1,2,3,5,3];
var b = [2,4,6,8];

// **************************************
// function search finds element in array and displays position if element exists

function search(word, arr) {
 
    var exists = false;

    for(var i=0; i < arr.length; i++){
        if(arr[i]==word){
            window.alert("Element exist on position "+i);
            exists = true;
        };
    };

    if(!(exists)){
        window.alert("Element doesn't exist in array.");
    };

};

// tests for search function 
// search(7, b)
// search(6, b)

// **************************************
// function searchB search element in array and returns Boolean value
 
function searchB(word, arr) {

    var exists = false;

    for(var i=0; i < arr.length; i++) {
        if(arr[i]==word){
            exists = true;
        };
    };
    
    return exists;
};

// tests for searchB function
// window.alert("6 exists in b? "+searchB(6,b));
// window.alert("7 exists in b? "+searchB(7,b));

// ***************************************
// function searchCount returns number of appearances of element in array
 
function searchCount(word, arr) {

    var counter = 0;

    for(var i=0; i < arr.length; i++) {
        if(arr[i]==word){
            counter+=1;
        };
    };
    
    return counter;
};

// tests for searchCount function
// window.alert("number of 3 in a? "+searchCount(3,a));
// window.alert("number of 11 in a? "+searchCount(11,a));

// ****************************************
// function iSection returns intersection array of two arrays
 
function iSection(arr1, arr2) {

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

    return arr3;
}

// tests for iSection function
// window.alert("intersection of a and b is : "+iSection(a,b));

// *******************************************
// function shuffle simply shuffles given array elements
     
function shuffle(b) {

    var temp = new Array();
    var templen;
    var take;
    while (b.length > 0) {
        take = Math.floor(Math.random()*b.length);
        templen = temp.push(b[take]);
        b.splice(take,1);
    };
    
    return temp;
};
    
// tests for shuffle function
// window.alert("shuffled b is : "+shuffle(b));


There are a lot of ways this code can be improved and porting like this is bad actually since JavaScript built-in functions are not used.

As an example, search function doesn't use built-in JavaScript indexOf method, in this situation:

app2.js
var a = [1,2,3,5,3];
var b = [2,4,6,8];

// **************************************
// function search2 finds element in array and displays position if element exists

function search2(word, arr) {
 
     var n = arr.indexOf(word);

    if(n>-1){
        window.alert("Element exist on position "+n);
    } else {
        window.alert("Element doesn't exist in the given array.");
    };
};

// tests for search function 
// search2(7, b)
// search2(6, b)


Similarly, there are better and much faster options for other functions too.

For second function which searches element in array and returns Boolean value True is element is present and False if element is not there, code is very simple:

searchB2 function:

// function searchB2 search element in array and returns Boolean value
 
function searchB2(word, arr) {

    return arr.indexOf(word)>-1;
    
};

// tests for searchB function
// window.alert("6 exists in b? "+searchB2(6,b));
// window.alert("7 exists in b? "+searchB2(7,b));

One more search array function I mentioned earlier is the one which returns number of appearances of given element in array.

searchCount2 function:

// function searchCount2 returns number of appearances of element in array
 
function searchCount2(word, arr) {

    var counter = 0;    
    var n = arr.indexOf(word);
    while(n>-1){
        counter ++;
        n = arr.indexOf(word, n+1);
    };
    
    return counter;
};

// tests for searchCount2 function
// window.alert("number of 3 in a? "+searchCount2(3,a));
// window.alert("number of 11 in a? "+searchCount2(11,a));


For solution on intersection function I recommend as further reading this post by Peter Scott.

*_*

 

template by blogger templates