Pages

Friday, December 24, 2010

JS - Find Position

/**
 * Find Index Position Algorithm
 * @author irfanudin ridho
 * @email  irfan.ub@gmail.com
 * @date   December 24, 2010
 * @version 1.0
 */

/*
 * This function will find the index of the minimum value
 * from given data
 *
 * @param data Array of data
 * @return number index of the min value
 */

function findMinPos(data){
    var minPos = 0;
    for(i=0;i<data.length;i++){
        if(data[i+1]<data[minPos]){
            minPos = i+1;
        }
    }
    return minPos;
}


/*
 * This function will find the index of the maximum value
 * from given data
 *
 * @param data Array of data
 * @return number index of the max value
 */

function findMaxPos(data){
    var maxPos = 0;
    for(i=0;i<data.length;i++){
        if(data[i+1]>data[maxPos]){
            maxPos = i+1;
        }
    }
    return maxPos;
}

No comments:

Post a Comment