我需要找到一系列编号的最大,最小,总和,平均和反向顺序。 创建一个网页,显示一系列no的输出(Max,Min,Sum,Average和Reverse Order)。该页面应包含一个表单,其中输入字段将出现一系列否。将在昏迷分隔的字段中输入。程序将继续进行计算,因为我继续提供一系列的。 样品:
输入数字系列:12,12,22,9,1,20 马克斯:22 最小:1 总和:76 平均值:12.66 逆序:20,1,9,22,12,12
我尝试的代码:
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title> Series Finder </title>
</head>
<body>
<table width="50%" border="0" align="center">
<tr>
<td width="13%">Input Series of Number :</td>
<td width="29%"><input name="box1" type="text" id="box1" /></td>
</tr>
<tr>
<td> </td>
<td></td>
</tr>
<tr>
<td>Max :</td>
<td><div id="result"> </div></td>
</tr>
<tr>
<td>Min :</td>
<td><div id="result"> </div></td>
</tr>
<tr>
<td>Sum :</td>
<td><div id="result"> </div></td>
</tr>
<tr>
<td>Average :</td>
<td><div id="result"> </div></td>
</tr>
<tr>
<td>Reverse Order :</td>
<td><div id="result"> </div></td>
</tr>
<tr>
<td> </td>
<td></td>
</tr>
</table>
</body>
</html>
<script>
function calculate() {
var myBox1 = document.getElementById('box1').value;
var res = Math.max([myBox1, myBox1]);
alert(res);
result.innerHTML = myResult;
}
</script>
答案 0 :(得分:3)
ECMAScript 2015解决方案:
const input = [12, 12, 22, 9, 1, 20];
let max = Math.max(...input);
let min = Math.min(...input);
let sum = input.reduce((r, i) => r + i, 0);
let average = (sum / input.length).toFixed(2);
let reverse = input.slice().reverse();
答案 1 :(得分:1)
function calculate() {
var values = document.getElementById('box1').value.split(/,/g);
var sum = values.reduce(function(a, b) { return parseInt(a) + parseInt(b); });
document.querySelector('#max').innerHTML = values.max();
document.querySelector('#min').innerHTML = values.min();
document.querySelector('#sum').innerHTML = sum;
document.querySelector('#avg').innerHTML = sum / values.length;
document.querySelector('#reverse').innerHTML = values.reverse().join(',');
}
Array.prototype.max = function() {
return Math.max.apply(null, this);
};
Array.prototype.min = function() {
return Math.min.apply(null, this);
};
&#13;
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title> Series Finder </title>
</head>
<body>
<table width="50%" border="0" align="center">
<tr>
<td width="13%">Input Series of Number:</td>
<td width="29%"><input name="box1" type="text" id="box1" value="12,12,22,9,1,20" /></td>
</tr>
<tr>
<td> </td>
<td></td>
</tr>
<tr>
<td>Max :</td>
<td><div id="max"> </div></td>
</tr>
<tr>
<td>Min :</td>
<td><div id="min"> </div></td>
</tr>
<tr>
<td>Sum :</td>
<td><div id="sum"> </div></td>
</tr>
<tr>
<td>Average :</td>
<td><div id="avg"> </div></td>
</tr>
<tr>
<td>Reverse Order :</td>
<td><div id="reverse"> </div></td>
</tr>
<tr>
<td> </td>
<td></td>
</tr>
</table>
<button onclick="calculate()">calculate</button>
</body>
</html>
&#13;