如何创建,在每一个点击按钮上发现平均一个javascript
问题描述:
如何创建一个JavaScript程序,发现在文本框中输入数字的平均每次查找按钮是点击如何创建,在每一个点击按钮上发现平均一个javascript
var all=[];
$(document).on('click', 'check', function()
{
var second, minute, hours, aht;
second = document.getElementById('sec').value;
minute = document.getElementById('min').value;
hours = document.getElementById('hour').value;
nocAll.push = (eval(second + (minute * 60) + (hours * 60 *60)));
for(var i =0; i< nocAll.length; i++);
sum += parseInt(nocAll.elmt[i], 10);
aht = sum/nocAll.length;
document.getElementById("AHT").innerHTML = aht;
})
这有试过
答
你编辑你的问题,所以这里是一个方法来做到这一点。
你在代码中有几个问题。
nocAll
是未定义(我想你的意思是使用all
Array.prototype.push
是一个函数,all.push(value)
var all = [];
// cache the elements you will be working with
var $second = $('#sec')
var $minute = $('#min')
var $hour = $('#hour')
var $aht = $("#AHT")
// add two numbers together
var add = function(a, b) {
return a + b
}
$(document).on('click', '.check', function() {
// get the current values of the inputs and convert to seconds
var hours = Number($hour.val()) * 60 * 60
var minutes = Number($minute.val()) * 60
var seconds = Number($second.val())
// add total time in seconds to the all array
all.push(hours + minutes + seconds)
// just for dubugging
console.log(all)
// calculate the average by folding the array and adding the values
// then divide by the length
$aht.html('average seconds:' + all.reduce(add, 0)/all.length)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="hour" placeholder="hours" />
<input type="text" id="min" placeholder="minutes" />
<input type="text" id="sec" placeholder="seconds" />
<button class="check">check</button>
<div id="AHT"></div>
请[编辑]你的问题,以显示你”我已经尝试并解释你遇到问题了。你是否坚持如何创建一个点击事件h安德勒,还是如何做数学? – nnnnnn