Created
March 5, 2010 14:44
-
-
Save javascripter/322762 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ValueFinder() { | |
} | |
ValueFinder.NotFoundError = function () { | |
Error.apply(this, arguments); | |
}; | |
ValueFinder.NotFoundError.prototype = new Error(); | |
ValueFinder.prototype.find = function (array) { | |
if (array.length == 0) { | |
throw new ValueFinder.NotFoundError("Not Found."); | |
} | |
var i; | |
var value = array[0]; | |
for (i = 1; i < array.length; i++) { | |
if (this.isTarget(array[i], value)) { | |
value = array[i]; | |
} | |
} | |
return value; | |
}; | |
ValueFinder.prototype.isTarget = function () { | |
throw new TypeError("You should override isTarget method."); | |
}; | |
var arrayMinFinder = new ValueFinder(); | |
arrayMinFinder.isTarget = function (a, b) { | |
return a < b; | |
}; | |
var arrayMaxFinder = new ValueFinder(); | |
arrayMaxFinder.isTarget = function (a, b) { | |
return a > b; | |
}; | |
function ArrayUtils() { | |
throw new TypeError("ArrayUtils is a static class."); | |
} | |
ArrayUtils.min = function (array) { | |
return arrayMinFinder.find(array); | |
}; | |
ArrayUtils.max = function (array) { | |
return arrayMaxFinder.find(array); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment