In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-15 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.