Skip to content

Instantly share code, notes, and snippets.

@javascripter
Created March 5, 2010 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javascripter/322762 to your computer and use it in GitHub Desktop.
Save javascripter/322762 to your computer and use it in GitHub Desktop.
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