Algorithms and Data Structures · Junior · Question 60 of 75
Why can Math.max(...arr) fail on a very large array?
- 1
Math.maxonly accepts up to 100 numbers as arguments - ✓Correct answer: Every element becomes an argument, and engines limit their number
- 3It returns the first element instead of the largest one
Explanation
Arguments are placed on the stack, so hundreds of thousands of them can throw a RangeError. For big arrays use a simple loop or reduce, which also stays O(n).