2017-10-05 86 views
0

我有一些代碼將輸入數字放入新數組中。我如何做到這一點,所以我的警報告訴用戶在輸入中代表了多少次這個數字?例如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>

回答

1

我已經改變了你的showTxt功能

function showTxt() { 
    var text = ""; 
    var input = document.getElementById("input").value; 
    var split = input.split(","); 
    var newArray = split; 
    var count; 
    for (var i = 0; i < newArray.length; i++) { 
    count = 0; 
    for (var j = 0; j < newArray.length; j++) { 
     if (newArray[i] === newArray[j]) { 
     count++; 
     } 
    } 
    alert("Number " + newArray[i] + " appears " + count + " times"); 
    } 
    text += newArray; 
    document.getElementById("print").innerHTML = text; 
} 
+0

非常感謝你:) –

1

您可以使用該方法從this gist像這樣:

window.onload = btn; 
 

 
function btn() { 
 
    document.getElementById("btn").onclick = showTxt; 
 
} 
 

 

 
function showTxt() { 
 
    var text = ""; 
 
    var input = document.getElementById("input").value; 
 
    var split = input.replace(/ /g, '').split(",").sort(); 
 
    compressArray(split); 
 
} 
 

 

 
function compressArray(original) { 
 
    var compressed = []; 
 
    // make a copy of the input array 
 
    var copy = original.slice(0); 
 

 
    // first loop goes over every element 
 
    for (var i = 0; i < original.length; i++) { 
 

 
    var myCount = 0; 
 
    // loop over every element in the copy and see if it's the same 
 
    for (var w = 0; w < copy.length; w++) { 
 
     if (original[i] == copy[w]) { 
 
     // increase amount of times duplicate is found 
 
     myCount++; 
 
     // sets item to undefined 
 
     delete copy[w]; 
 
     } 
 
    } 
 

 
    if (myCount > 0) { 
 
     var a = new Object(); 
 
     a.value = original[i]; 
 
     a.count = myCount; 
 
     compressed.push(a); 
 
    } 
 
    } 
 
    for (var i = 0; i < compressed.length; i++) { 
 
    var message = compressed[i].value + ' appears ' + compressed[i].count + ' times.'; 
 
    alert(message); 
 
    document.getElementById("print").innerHTML += message + '</br>'; 
 
    } 
 
};
<input id="input" type="text"> 
 
<button id="btn" type="button">Show</button> 
 
<p id="print"></p>

0

看看這個,可能會有幫助。

<!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; 
 
     let countedValues = newArray.reduce(function(obj,val){ 
 
     if(obj[val]) 
 
      obj[val] += 1; 
 
     else 
 
      obj[val] = 1; 
 
     return obj 
 
     }, {}) 
 
\t \t \t for (let value in countedValues) { 
 
\t \t \t \t alert("Number " + value + " appears " + countedValues[value] + " 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>