Find Max Number

 function find_max(nums) {

let max_num = Number.NEGATIVE_INFINITY; // smaller than all other numbers for (let num of nums) { if (num > max_num) { max_num = num; // Update max_num with the current number if it's greater } } return max_num; }


In this code, max_num starts as Number.NEGATIVE_INFINITY, which is smaller than any other number. The loop iterates through each number in the nums array and compares it with the current max_num. If the current number is greater, it updates max_num to the new maximum. After the loop finishes, the function returns the maximum number found.



Comments

Popular posts from this blog

Basics

Arguments Passed in Functions

Offset index of an array