<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo</title>
<script>
window.onload = function(){
var display = document.getElementById('display');
var arr =/\[(.+)\]/g.exec(document.getElementById('monitor').innerHTML)[1].split(',');
var getMax = function(arr){
var max = arr[0],len=arr.length,i=1;
for(;i<len;i++)
max = arr[i] > max ? arr[i] : max;
return max;
};
document.getElementById('max').onclick = function(){
display.innerHTML = 'max:' + getMax(arr);
}
document.getElementById('sumMax').onclick = function(){
display.innerHTML = 'sumMax:' + getMax([getMax(arr),eval(arr.join('+'))]);
}
}
</script>
</head>
<body>
<div id="monitor">array = [-1,3,-5,4,-6,8]</div>
<div id="display"></div>
<input type="button" id="max" value="Max:">
<input type="button" id="sumMax" value="sumMax:">
</body>
</html>
温馨提示:内容为网友见解,仅供参考