【发布时间】:2019-08-12 21:41:10
【问题描述】:
我正在尝试编写一个代码,该代码将根据用户输入评估列表中的数字,并计算该列表的总和、平均值、最小值和最大值。我已经从别人的帮助中得到了总和部分。我似乎无法找到如何从列表中获取最大和最小数字。我试图将所有功能(求和、平均值、最大值和最小值)作为按钮,就像代码中已经存在的求和按钮一样,当点击它时会提醒用户该特定功能。
.title { font-weight:bold; margin-top:1em; }
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<!--- This only allows the user to input numbers --->
<input type='number' id='input'>
<!--- This is the button that adds the number to the list --->
<input type='button' value='add to list' id='add' disabled="disabled">
<!--- This will list all of the numbers in the list --->
<div class="title">Topics</div>
<ul id='list'></ul>
<!--- When clicked, this will alert the user with the sum of their numbers --->
<button id="something">Click Here To See The Sum</button>
<script>
let list = document.getElementById("list");;
let btn = document.getElementById("something");
let input = document.getElementById("input");
let add = document.getElementById("add");
var sum = 0;
input.addEventListener("input", enableDisable);
btn.addEventListener("click", sumvar);
add.addEventListener("click", function() {
var li = document.createElement("li");
li.textContent = input.value;
sum += +input.value;
list.appendChild(li);
input.value = "";
add.disabled = "disabled";
});
// This allows the "add to list" button to be turned on/off depending if the user has typed in a number
function enableDisable(){
if(this.value === ""){
add.disabled = "disabled";
} else {
add.removeAttribute("disabled");
}
}
// This function will alert the user of the sum of their numbers
function sumvar() {
alert("The sum of your numbers is: " + sum);
}
</script>
</body>
</html>
【问题讨论】:
标签: javascript html list user-input