Examples
Write findMax so that the following cases hold.
- Input
findMax([3,7,2,9,4])Output9 - Input
findMax([-3,-7,-2])Output-2 - Input
findMax([42])Output42
Answer
text
function findMax(nums) {
return Math.max(...nums);
}
findMax([3, 7, 2, 9, 4]); // 9
For very large arrays, prefer a reduce loop over spreading into Math.max to avoid argument limits.
