cleaner max function, remove unnecessary duplication

This commit is contained in:
Aditya Bhargava
2022-11-18 14:18:55 -06:00
parent 7d27d15c0d
commit 0292985401

View File

@@ -2,25 +2,15 @@
* Calculate the largest number
* This solution only works for arrays longer than one
* @param {Array} array Array of numbers
* @returns {number} The argest number
* @returns {number} The largest number
*/
const max = array => {
if (array.length === 2) return array[0] > array[1] ? array[0] : array[1];
const max = (array) => {
if (array.length === 0) return 0;
if (array.length === 1) {
return array[0];
}
const subMax = max(array.slice(1));
return array[0] > subMax ? array[0] : subMax;
};
/**
* Calculate the largest number
* This solution works for arrays of any length
* @param {Array} array Array of numbers
* @param {number} max Maximum value
* @returns {number} The argest number
*/
const alternativeSolutionMax = (array, max = 0) =>
array.length === 0
? max
: alternativeSolutionMax(array.slice(1), array[0] > max ? array[0] : max);
console.log(max([1, 5, 10, 25, 16, 1])); // 25
console.log(alternativeSolutionMax([1, 5, 10, 25, 16, 1])); // 25