我有一些代碼將輸入數字放入新數組中。我如何做到這一點,所以我的警報告訴用戶在輸入中代表了多少次這個數字?例如0,4,4,2,3,4,1會顯示「0出現1次,4出現3次」等等......我認爲我接近但不能得到最後的部分權利...計數值的計數次數
<!DOCTYPE html>
<html>
<head>
\t <title>Oppgave 5</title>
\t <script type="text/javascript">
\t \t
\t \t window.onload = btn;
\t \t function btn() {
\t \t \t document.getElementById("btn").onclick = showTxt;
\t \t }
\t \t function showTxt() {
\t \t \t var text = "";
\t \t \t var input = document.getElementById("input").value;
\t \t \t var split = input.split(",");
\t \t \t var newArray = split;
\t \t \t var count = 0;
\t \t \t for (var i = 0; i < newArray.length; i++) {
\t \t \t \t if (newArray[i] === parseInt(input)) {
\t \t \t \t \t count++;
\t \t \t \t }
\t \t \t \t alert("Number " + newArray[i] + " appears " + count + " times");
\t \t \t }
\t \t \t text += newArray;
\t \t \t document.getElementById("print").innerHTML = text;
\t \t }
\t </script>
</head>
<body>
<input id="input" type="text">
<button id="btn" type="button">Show</button>
<p id="print"></p>
</body>
</html>
非常感謝你:) –