Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to find the maximum value of an array by javascript

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces javascript how to find the maximum value of the array, the article is very detailed, has a certain reference value, interested friends must read it!

Javascript to find the maximum value of the array method: 1, declare a save the largest element of the variable max;2, traverse the array and compare each array element with max; 3, when the array element is greater than max, the array element is stored in max; 4, output max.

This article operating environment: windows7 system, javascript1.8.5 version, DELL G3 computer

JavaScript calculates the maximum value of elements in an array

Request:

Find the maximum value in the array [2, 6, 1, 7, 52, 25, 7].

The idea of realization is:

Declare a variable max that holds the largest element

The default maximum value max is defined as the first element in the array, arr [0]

Iterate through the array and compare each array element with max

If the array element is greater than max, store the array element in max, otherwise continue to the next round of comparison

Finally, output this max

Code implementation: var arr = [2, 6, 1, 77, 52, 25, 7, 99]; var max = arr [0]; for (var I = 1; I < arr.length; iTunes +) {if (max < arr [I]) {max = arr [I];}} console.log (max); output: 99 function version: code implementation: / / use function to find the maximum value in the array [3,77,44,99,143]. Function getArrMax (arr) {/ / arr accepts an array arr = [3,77,44,99,143] var max = arr [0]; for (var I = 1; I < arr.length; iTunes +) {if (max < arr [I]) {max = arr [I];}} return max;} / / getArrMax ([3,77,44,99,143]) / argument is an array var re = getArrMax ([3,77,44,99,143]); console.log (re); output: 143Function arguments: code implementation: function getMax () {var max = arguments [0]; for (var I = 1; I < arguments.length; iTunes +) {if (max < arguments [I]) {max = arguments [I];} return max } console.log (getMax (1,2,3)); console.log (getMax (1,2,3,4,5)); console.log (getMax (11,2,34,444,5100)); output: more than 35444 is all the content of the article "how to find the maximum value of javascript array". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report