Skip to content

Commit 00b2847

Browse files
TrayHardIlia Poliakov
andauthored
Update 04_recursive_max.js with fix for alternative solution (egonSchiele#262)
* Update 04_recursive_max.js with fix for alternative solution Current alternative solution works only for arrays of natural numbers and for empty arrays it returns 0 instead of null or Error. This commit fixes these problems. * Considering comments from the author --------- Co-authored-by: Ilia Poliakov <ilia.poliakov@macys.com>
1 parent 440db4f commit 00b2847

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

04_quicksort/javascript/04_recursive_max.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@ function max(array) {
1414

1515
/**
1616
* Calculate the largest number
17-
* This solution works for arrays of any length
17+
* This solution works for arrays of any length and returns the smallest possible number for empty arrays
1818
* @param {Array} array Array of numbers
1919
* @param {number} max Maximum value
20-
* @returns {number} The argest number
20+
* @returns {number} The largest number
2121
*/
22-
function alternativeSolutionMax(array, max = 0) {
22+
function alternativeSolutionMax(array, max = Number.MIN_VALUE) {
2323
return array.length === 0
2424
? max
2525
: alternativeSolutionMax(array.slice(1), array[0] > max ? array[0] : max);
2626
}
2727

2828
console.log(max([1, 5, 10, 25, 16, 1])); // 25
2929
console.log(alternativeSolutionMax([1, 5, 10, 25, 16, 1])); // 25
30+
31+
console.log(max([])); // RangeError: Maximum call stack size exceeded
32+
console.log(alternativeSolutionMax([])); // Number.MIN_VALUE

0 commit comments

Comments
 (0)