Skip to content

Commit

Permalink
Merge pull request #90 from Xuefeng-Zhu/patch-9
Browse files Browse the repository at this point in the history
simplify min and max with reduce
  • Loading branch information
mateogianolio authored Dec 25, 2016
2 parents 1e830e1 + c293edd commit 94fbae7
Showing 1 changed file with 6 additions and 24 deletions.
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

0 comments on commit 94fbae7

Please sign in to comment.