Home > Web Front-end > JS Tutorial > How to get the maximum and minimum values ​​of an array in JavaScript_javascript tips

How to get the maximum and minimum values ​​of an array in JavaScript_javascript tips

WBOY
Release: 2016-05-16 15:31:22
Original
1195 people have browsed it

The example in this article describes the stretch and shrink menu code implemented in JavaScript. Share it with everyone for your reference. The details are as follows:
The screenshot of the running effect is as follows:

The specific code is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>JavaScript获取数组最大值和最小值</title> 
<script type="text/javascript"> 
  //获取数组最大值 
  function findMax() 
  { 
    var arr = [3,4,76,2,54,23,78,90,89,99,234,12,23,76,89,90]; 
    var max = Array.max(arr); 
    document.getElementById("max").value = max; 
  } 
   
  //获取数组最大值函数 
  Array.max = function(arr) 
  { 
    return Math.max.apply(Math,arr);   
  } 
   
  //获取数组最小值 
  function findMin() 
  { 
    var arr = [34,4,3,67,89,30,23]; 
    var min = Array.min(arr); 
    document.getElementById("min").value = min;  
  } 
   
  //获取数组最小值函数 
  Array.min = function(arr) 
  { 
    return Math.min.apply(Math,arr);   
  } 
   
  window.onload = function(){ 
    findMax(); 
    findMin(); 
  }; 
</script> 
</head> 
 
<body> 
  <label>数组最大值:</label> 
  <input type="text" id="max"/> 
  <label>数组最小值:</label> 
  <input type="text" id="min"/> 
</body> 
</html> 
Copy after login

I hope this article will be helpful to everyone learning JavaScript programming.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template