Lesedi Ramahobo     About     Tags     Apps     Feed

Find the greatest number - JavaScript style

For this bin exercise I'll show how you can find the largest number in an array using JavaScript's reduce function.

JS Bin on jsbin.com

This example runs well on natural numbers (step 1), but it will break if our array is made up negative numbers (step 2). It will simply print out zero is the highest number even though zero is not part of our input array.

To fix this (step 3), we pass in '-Infinity' to give our reduce function the lowest possible number to start with.

If you liked this post, you can share it with your followers or follow me on Twitter!

#Javascript #Programming