我只想随机点击某个按钮,然后当我点击 Sum 按钮时,它会显示点击按钮值结果的总和,我需要这个带有 java 脚本的代码。
<!DOCTYPE html>
<html>
<head>
<script>
var mynum = new Array(6);
var i = 0;
unction buttonClicked(obj) {
if (i < mynum.length)
mynum[i++] = obj.value;
else alert("Array limit reached");
}
function sum() {
sumOfArray = 0;
for (j = 0; j < i; j++)
sumOfArray += parseInt(mynum[j++]);
alert("Sum = " + sumOfArray);
}
</script>
</head>
<body>
<p id="demo">Result</p>
button type="button" onclick="buttonClicked(2)">1</button>
<button type="button" onclick="buttonClicked(3)">2</button>
<button type="button" onclick="buttonClicked(3)">4</button>
<button type="button" onclick="buttonClicked(3)">8</button>
<button type="button" onclick="sum()">Sum</button>
</body>
</html>