From cd809ea0e6b88d8cd13320ca672a36c7afb442a7 Mon Sep 17 00:00:00 2001 From: Peter Marsh Date: Wed, 3 May 2023 16:08:35 +0100 Subject: [PATCH 1/2] change implementation of min and max. No longer using apply which causes stack error, replaced with simple for loop --- src/math/calculation.js | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/math/calculation.js b/src/math/calculation.js index d83cb66a59..bf79e27ae0 100644 --- a/src/math/calculation.js +++ b/src/math/calculation.js @@ -498,11 +498,19 @@ p5.prototype.map = function(n, start1, stop1, start2, stop2, withinBounds) { * @param {Number[]} nums Numbers to compare * @return {Number} */ -p5.prototype.max = function(...args) { +p5.prototype.max = function (...args) { + const findMax = arr => { + let max = -Infinity; + for (let x of arr) { + max = x > max ? x : max; + } + return max; + }; + if (args[0] instanceof Array) { - return Math.max.apply(null, args[0]); + return findMax(args[0]); } else { - return Math.max.apply(null, args); + return findMax(args); } }; @@ -546,11 +554,19 @@ p5.prototype.max = function(...args) { * @param {Number[]} nums Numbers to compare * @return {Number} */ -p5.prototype.min = function(...args) { +p5.prototype.min = function (...args) { + const findMin = arr => { + let min = Infinity; + for (let x of arr) { + min = x < min ? x : min; + } + return min; + }; + if (args[0] instanceof Array) { - return Math.min.apply(null, args[0]); + return findMin(args[0]); } else { - return Math.min.apply(null, args); + return findMin(args); } }; From 7d5200dca78279fc0acbdfb113bc0bb0d626ede4 Mon Sep 17 00:00:00 2001 From: Peter Marsh Date: Wed, 3 May 2023 16:35:30 +0100 Subject: [PATCH 2/2] removed some whitespace space to make formatting consistent --- src/math/calculation.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/math/calculation.js b/src/math/calculation.js index bf79e27ae0..364ddc730a 100644 --- a/src/math/calculation.js +++ b/src/math/calculation.js @@ -498,7 +498,7 @@ p5.prototype.map = function(n, start1, stop1, start2, stop2, withinBounds) { * @param {Number[]} nums Numbers to compare * @return {Number} */ -p5.prototype.max = function (...args) { +p5.prototype.max = function(...args) { const findMax = arr => { let max = -Infinity; for (let x of arr) { @@ -554,7 +554,7 @@ p5.prototype.max = function (...args) { * @param {Number[]} nums Numbers to compare * @return {Number} */ -p5.prototype.min = function (...args) { +p5.prototype.min = function(...args) { const findMin = arr => { let min = Infinity; for (let x of arr) {