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));
Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.
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.
Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.
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]