计算表示值的次数

时间:2017-10-05 14:53:40

标签: javascript arrays

我有一些代码,它接受输入数字并将它们放入一个新数组中。我该怎么做才能让我的警报告诉用户输入中数字的表示次数是多少?示例0,4,4,2,3,4,1将显示" 0出现1次,4出现3次" ......等等......我认为我很接近,但不能让最后一部分正确......



<!DOCTYPE html>
<html>
<head>
	<title>Oppgave 5</title>
	<script type="text/javascript">
		
		window.onload = btn;

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

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

	</script>
</head>
<body>
<input id="input" type="text">
<button id="btn" type="button">Show</button>
<p id="print"></p>
</body>
</html>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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>

答案 2 :(得分:0)

检查一下,可能会有帮助。

&#13;
&#13;
<!DOCTYPE html>
<html>
<head>
	<title>Oppgave 5</title>
	<script type="text/javascript">
		
		window.onload = btn;

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

		function showTxt() {
			var text = "";
			var input = document.getElementById("input").value; 
			var split = input.split(",");
			var newArray = split;
      let countedValues = newArray.reduce(function(obj,val){
        if(obj[val])
          obj[val] += 1;
        else 
          obj[val] = 1;
        return obj
      }, {})
			for (let value in countedValues ) {
				alert("Number " + value + " appears " + countedValues[value] + " times");
			}
			text = newArray;
			document.getElementById("print").innerHTML = text;
		}

	</script>
</head>
<body>
<input id="input" type="text">
<button id="btn" type="button">Show</button>
<p id="print"></p>
</body>
</html>
&#13;
&#13;
&#13;