JS get the maximum value in the array

1. Set an empty variable num, loop the array, assign the item to num as long as the array item is greater than, and finally return num

       function maxV(arr) {
    
    
            let num = 0;
            arr.forEach(item => {
    
    
                if(item > num) {
    
    
                    num = item
                }
            })
            return num
        }
        const arr = [3,4,5,77,1,87,100]
        console.log(maxV(arr))   // 100

2. Take the last item after sorting

const arr = [3,4,5,77,1,87,100]
console.log(arr.sort((a,b) => a -b)[arr.length -1])   // 100

Guess you like

Origin blog.csdn.net/YL971129/article/details/113837256