我无法让我的javascript号码分拣机工作

时间:2015-02-10 10:49:57

标签: javascript

我正在制作一个数字分拣机,提醒不同数字的数量(例如,11223将返回为1的数量= 2,两个数量= 2,三个数量= 1等等。)

这是代码

<html>
<body>
<textarea width="100" height="50" id="text" onBlur="sort();"></textarea>
<script>
var text = document.getElementById("text").value;
var no1 = 0
var no2 = 0
var no3 = 0
var no4 = 0
var no5 = 0
var no6 = 0
var no7 = 0
var no8 = 0
var no9 = 0
var no0 = 0
var other = 0
var split = text.split("");
function sort() {
    for (i = 1; i < split; i++) {
        if (string[i]===1) {
            no1++;
            return no1;
        }
        else if (string[i]===2) {
            no2++;
            return no2;
        }
        else if (string[i]===3) {
            no3++;
            return no3;
        }
        else if (string[i]===4) {
            no4++;
            return no4;
        }
        else if (string[i]===5) {
            no5++;
            return no5;
        }
        else if (string[i]===6) {
            no6++;
            return no6;
        }
        else if (string[i]===7) {
            no7++;
            return no7;
        }
        else if (string[i]===8) {
            no8++;
            return no8;
        }
        else if (string[i]===9) {
            no9++;
            return no9;
        }
        else if (string[i]===0) {
            no0++;
            return no0;
        }
        else {
            other++;
            return other;
        }
    }
    alert("number of ones = " + no1 + ", number of twos = " + no2 + ", number of threes = " + no3 + ", number of fours = " + no4 + ", number of fives = " + no5 + ", number of sixes = " + no6 + ", number of sevens = " + no7 + ", number of eights = " + no8 + ", number of nines = " + no9 + ", number of zeros = " + no0 + ", number of other characters = " + other + ".");
}
</script>
</body>
</html>

当我输入一个值并单击远离文本字段时,它会为所有变量返回0

请帮助

2 个答案:

答案 0 :(得分:0)

您的代码维护起来不是很容易,所以我制作了自己的版本,试试看:

function sort() {
    var text = document.getElementById("text").value;
    // will contain a list of chars with their associated count
    var charCounter = {};
    for (var i = 0, l=text.length; i < l; i++) {
        var char = text[i];
        // if char is a number
        if(!isNaN(parseInt(char))){
            if(charCounter[char]){ charCounter[char]++; }
            else { charCounter[char] = 1; }
        // if char is not a number
        } else {
            if(charCounter['other characters']){ charCounter['other characters']++; }
            else { charCounter['other characters'] = 1; }
        }
    }
    outputList(charCounter);
}

function outputList(obj){
    var keys = Object.keys(obj);
    var output = '';
    for(var i=0, l=keys.length; i<l; i++){
        output += 'Number of ' + keys[i] + ' : ' + obj[keys[i]] + '.\n';
    }
    alert(output);
}

JS Fiddle Demo

答案 1 :(得分:0)

更简单的解决方案是对数字名称使用一个单词数组,并将其用于对象上的键,这些键会累积每个数字的出现次数,例如

function countDigits(n) {

  // Array of number names
  var words = ['zeros','ones','twos','threes','fours',
               'fives','sixes','sevens','eights','nines'];

  // Split number into digits
  var nums = String(n).split('');
  var counts = {};

  // Count how many of each digit
  nums.forEach(function(n){
    if (!(counts.hasOwnProperty(words[n]))) counts[words[n]] = 0;
    ++counts[words[n]];
  });

  // Write to output
  words.forEach(function(w){
    console.log('Number of ' + w + ' = ' + (counts[w] || 0) + '\n');
  });
}

countDigits(1012023405);

/*
  Number of zeros = 3
  Number of ones = 2
  Number of twos = 2
  Number of threes = 1
  Number of fours = 1
  Number of fives = 1
  Number of sixes = 0
  Number of sevens = 0
  Number of eights = 0
  Number of nines = 0
*/

或略有不同的表述:

function countDigits(n) {

  // Array of number names
  var words = ['zeros','ones','twos','threes','fours',
               'fives','sixes','sevens','eights','nines'];
  var counts = [];

  // Split number into digits and count
  String(n).split('').forEach(function(n) {
    counts[n] = counts[n]? counts[n] + 1 : 1;
  });

  // Write to output
  for (var i=0; i<10; i++) {
    console.log('Number of ' + words[i] + ' = ' + (counts[i] || 0) + '\n');
  }
}