我有一个工资选择列表,我试图找到所有数组值的总和大于从列表中选择的值。我无法获取输出总和的代码。
<select id='filter' name=''>
<option value = '0'>0</option>
<option value = '30000'>30000</option>
<option value = '35000'>35000</option>
<option value = '40000'>40000</option>
<option value = '45000'>45000</option>
<option value = '50000'>50000</option>
<option value = '55000'>55000</option>
</select>
<button id='go' class=''>GO</button>
<input id='result' name='' value='' class=''>
<input id='count' name='' value='' class=''>
<script src='js/javascript 15.js'></script>
transform
答案 0 :(得分:2)
看起来有一些事情需要改变:
numbers
需要更改为salaries
(因为数字不是代码中定义的变量)
30
需要更改为numberFilter
,因为您要查找的所有数组值的总和大于numberFilter
,而不是30
您只需更新一次结果:计算完总和后。您不需要在for循环的每次迭代期间更新结果。在每次迭代期间进行更新仍然有效,但既不必要也不高效。
更好的间距
所以你的代码会变成
salaries = [59360, 37012, 30301, 39738, 39169, 52832, 42132, 41220, 36901, 54497, 53108, 32553, 43023, 51505, 31499];
document.getElementById('go').onclick = function () {
var numberFilter = document.getElementById('filter').value;
total = 0;
for (index = 0; index < salaries.length; index++) {
if (salaries[index] > numberFilter) {
total = total + salaries[index];
};
};
document.getElementById('result').value = total;
};
答案 1 :(得分:1)
const numberFilter = 30000
let salaries = salaries.filter(salary => salary > numberFilter)
您可以先过滤掉您感兴趣的数字。
例如,数字大于 30000
const salariesSum = salaries.reduce((prev, current) => prev + current)
然后总结一下:
const numberFilter = 30000
const salariesSum = salaries.filter(salary => salary > numberFilter).reduce((a, b) => a + b)
或oneliner:
javac -source 1.7 -target 1.7 HttpsTest.java SSLSocketFactoryEx.java
答案 2 :(得分:0)
看起来您只需要引用salaries
数组而不是numbers
,因为numbers
未声明。然后你需要添加一些比较逻辑来获得大于过滤器数的任何东西。
var salaries = [59360, 37012, 30301, 39738, 39169, 52832, 42132, 41220, 36901, 54497, 53108, 32553, 43023, 51505, 31499];
document.getElementById('go').onclick = function () {
var numberFilter = document.getElementById('filter').value;
var total = 0;
for (index = 0; index < salaries.length; index++) {
if (salaries[index] > numberFilter) {
total = total + salaries[index];
};
};
//Write the result after the loop is complete
document.getElementById('result').value = total;
};
答案 3 :(得分:0)
您可以使用filter和reduce数组原型来创建总和。您还需要将事件侦听器附加到select
,否则您的脚本将只使用var salaries = [59360, 37012, 30301, 39738, 39169, 52832, 42132, 41220, 36901, 54497, 53108, 32553, 43023, 51505, 31499];
document.getElementById('filter').addEventListener('change', event => {
var filteredSalaries = salaries.filter(salary => { return salary > event.target.value });
var sum = filteredSalaries.reduce( (salary, total) => { return total + salary}, 0);
console.log('sum', sum);
console.log('salaries greater than filter', filteredSalaries);
document.getElementById('result').value = sum;
});
的初始值运行一次。
<select id='filter' name=''>
<option value = '0'>0</option>
<option value = '30000'>30000</option>
<option value = '35000'>35000</option>
<option value = '40000'>40000</option>
<option value = '45000'>45000</option>
<option value = '50000'>50000</option>
<option value = '55000'>55000</option>
</select>
<input id="result" value="" />
&#13;
let salaries = [59360, 37012, 30301, 39738, 39169, 52832, 42132, 41220, 36901, 54497, 53108, 32553, 43023, 51505, 31499]
&#13;
答案 4 :(得分:-1)
我建议将onclick事件处理程序直接添加到“go”按钮元素并调用以下函数(我也在计算中添加了计数)。
<script type="text/javascript" language="javascript">
var salaries = [59360, 37012, 30301, 39738, 39169, 52832, 42132, 41220, 36901, 54497, 53108, 32553, 43023, 51505, 31499];
function performCalc()
{
var listElem = null;
var selectedValue = 0;
var index = 0;
var total = 0;
var count = 0;
var resultElem = null;
var countElem = null;
try
{
listElem = document.getElementById("filter");
if(listElem!=null)
{
selectedValue = parseInt(listElem.options[listElem.selectedIndex].value);
for (index = 0; index < salaries.length; index++)
{
if (salaries[index] > selectedValue)
{
total += salaries[index];
count++;
}
}
resultElem = document.getElementById("result");
resultElem.value = total.toString();
countElem = document.getElementById("count");
countElem.value = count.toString();
}
}
catch(e)
{
alert("performCalc Error: " + e.Message);
}
finally
{
}
}
</script>
<button id = 'go' class = '' onclick="performCalc()">GO</button>