Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify min and max with reduce #90

Merged
merged 2 commits into from
Dec 25, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 6 additions & 24 deletions vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,37 +382,19 @@
* @returns {Number} the smallest element of the current vector
**/
Vector.prototype.min = function () {
var min = Number.POSITIVE_INFINITY,
data = this.data,
value,
i, l;

for (i = 0, l = data.length; i < l; i++) {
value = data[i];
if (value < min)
min = value;
}

return min;
return this.reduce(function(acc, item) {
return Math.min(acc, item);
}, Number.POSITIVE_INFINITY);
};

/**
* Gets the maximum value (largest) element of current vector.
* @returns {Number} the largest element of current vector
**/
Vector.prototype.max = function () {
var max = Number.NEGATIVE_INFINITY,
data = this.data,
value,
i, l;

for (i = 0, l = this.length; i < l; i++) {
value = data[i];
if (value > max)
max = value;
}

return max;
return this.reduce(function(acc, item) {
return Math.max(acc, item);
}, Number.NEGATIVE_INFINITY);
};

/**
Expand Down