From d23dcb46d0269b1f8d3c80a043be0f630603deb5 Mon Sep 17 00:00:00 2001 From: Xuefeng Zhu <xzhu15@illinois.edu> Date: Wed, 21 Dec 2016 00:12:10 -0600 Subject: [PATCH 1/2] simplify min and max with reduce --- vector.js | 30 ++++++------------------------ 1 file changed, 6 insertions(+), 24 deletions(-) diff --git a/vector.js b/vector.js index 51ae4847..ec608be2 100644 --- a/vector.js +++ b/vector.js @@ -382,18 +382,9 @@ * @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); }; /** @@ -401,18 +392,9 @@ * @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); }; /** From c293edde099e02b6a8cc03a5f7503ab24f4fe3b9 Mon Sep 17 00:00:00 2001 From: Xuefeng Zhu <xzhu15@illinois.edu> Date: Wed, 21 Dec 2016 00:24:13 -0600 Subject: [PATCH 2/2] fix typo --- vector.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vector.js b/vector.js index ec608be2..33b092aa 100644 --- a/vector.js +++ b/vector.js @@ -384,7 +384,7 @@ Vector.prototype.min = function () { return this.reduce(function(acc, item) { return Math.min(acc, item); - }), Number.POSITIVE_INFINITY); + }, Number.POSITIVE_INFINITY); }; /** @@ -394,7 +394,7 @@ Vector.prototype.max = function () { return this.reduce(function(acc, item) { return Math.max(acc, item); - }), Number.NEGATIVE_INFINITY); + }, Number.NEGATIVE_INFINITY); }; /**