constarray=[-29,5,0,3,111,99,2,99];functionmaxNumberInArray(arr){letmaxNumber=arr[0];//same way can apply on other loop like(For In, For Of, While)for(leti=0;i<arr.length;i++){if(arr[i]>maxNumber){maxNumber=arr[i];}}returnmaxNumber;}console.log(maxNumberInArray(array));
constarray=[-1,10,30,45,5,6,89,17];// `(previousValue, currentValue, index, array) => nextValue`// Notice how all data (`currentValue`, `index`, `array`) // associated with the array being folded is "bunched up" // to the right/end of the parameter list.constselectMax=(previous,value)=>value>previous?value:previous;// 1. `initialValue` is optional without it `array[0]` // becomes the `initialValue` and processing starts // at `array[1]`// 2. Without `initialValue` an empty array will cause:// `TypeError: Reduce of empty array with no initial value`// So empty arrays have to be handled separatelyconstmaxValue=array.length>0?array.reduce(selectMax):undefined;console.log(maxValue);
Technically using Number.NEGATIVE_INFINITY would be logically improper if the array had no elements,
While I agree that is exactly what happens with Math.max():
Handling negative numbers could be achieved by using Number.NEGATIVE_INFINITY for initialValue (instead of 0), or even just leaving initialValue empty (as reduce() then uses the first element of the array). Technically using Number.NEGATIVE_INFINITY would be logically improper if the array had no elements, but you'd get a TypeError then anyway.
That said, I don't think element and max being around the wrong way is a practical issue; they are by convention (and for code readability should be swapped), but functionally this implementation of callbackFn will still work as the only thing that matters is that the greater of the two args is returned. It's something I'd punt back in a code review, but it'd pass tests, so to speak.
I think what's funny is that with javascript's dynamic typing sometimes it is a good idea to ensure the array you are using is in fact an array of numbers, i.e.
Top comments (5)
Put differently:
While I agree that is exactly what happens with
Math.max()
:Perhaps that is consistent with
i.e. return a value that is "a number" put invalidates most following numeric operations without forcing the script to stop (throw an error).
With recursion (just for completeness):
Handling negative numbers could be achieved by using
Number.NEGATIVE_INFINITY
for initialValue (instead of 0), or even just leaving initialValue empty (asreduce()
then uses the first element of the array). Technically usingNumber.NEGATIVE_INFINITY
would be logically improper if the array had no elements, but you'd get a TypeError then anyway.That said, I don't think
element
andmax
being around the wrong way is a practical issue; they are by convention (and for code readability should be swapped), but functionally this implementation ofcallbackFn
will still work as the only thing that matters is that the greater of the two args is returned. It's something I'd punt back in a code review, but it'd pass tests, so to speak.I think what's funny is that with javascript's dynamic typing sometimes it is a good idea to ensure the array you are using is in fact an array of numbers, i.e.
would produce "Bob" as the max number, which is hilarious :D
Do you think about this way: array.sort()[0]