Tuesday 31 August 2010

arrayValueExistsAdapted(anArray, aValue) JavaScript

/**
 * Tests to see if aValue exists in anArray. Doesn't check to see if the whole of aValue exists but instead looks at the 
 *   string which comes before a colon punctuation mark (":") within each string element within the array.
 * @param anArray - an string array which is parsed, the element which is tested is anything before a colon in each 
 *   element of the array.
 * @param aValue - a value which is compared to the part of the element of anArray which comes before the colon 
 *   punctuation mark.
 * returns true if aValue is found within anArray, else return false. 
 */
function arrayValueExistsAdapted(anArray, aValue){
  var found = false;
  for (var i = 0; i < anArray.length; i++){
    var crumb1 = anArray[i].split(":");
    if (crumb1[0] == aValue){
      found = true;
    }
  }
  return found;
}

Adapted from arrayValueExists(anArray, aValue):

/**
 * Test to see if aValue is present within the elements of anArray, returns true if it does, 
 *   else returns false.
 * @param anArray
 * @param aValue
 */
function arrayValueExists(anArray, aValue){
  var found = false;
  for (var i = 0; i < anArray.length; i++){
    if (anArray[i] == aValue){
      found = true;
    }
  }
  return found;
}

1 comment: