What is meaning of useing (-Infinity, Infinity) in sorting | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is meaning of useing (-Infinity, Infinity) in sorting

function myFunction(arr) {<br> var len = arr.lenght;<br> var max = -Infinity;<br> while(len--) {<br> &nbsp;&nbsp; if(arr[len] &gt; max) {<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; max = arr[len];<br> &nbsp;&nbsp;&nbsp; }<br> }<br> &nbsp; return max;<br> }

2nd Oct 2022, 11:50 AM
A Rahman Mamnoon
A Rahman Mamnoon - avatar
1 Answer
+ 2
The code you attached is not readable, invalid JavaScript code (probably copy-pasted from somewhere). It is better to save your program in the web playground, and provide a link with "Insert code" feature. -Infinity might be used in this snippet to represent a number that is smaller than any other number. So that the algorithm is always able to do a meaningful comparison on all elements of the list, and always return a result.
2nd Oct 2022, 4:36 PM
Tibor Santa
Tibor Santa - avatar