使用javascript计算重复的单词

时间:2017-03-03 17:25:40

标签: javascript

我正在尝试解决这个问题,但我有点卡住,这就是我现在所拥有的,我正在尝试迭代增加索引的字符串加上每个字一个

  

"编写一个函数,使用计数执行基本字符串压缩   重复的字符,例如" aabcccccaaa"会成为" a2b1c5a3",如果   只是压缩的字符串不会小于原始字符串   打印原文"



function countWords() {
  var word = "aabcccccaaa";
  var result = "";
  var counter = 0;

  for (var i = 0; i <= word.length; i++) {
    if (word[i] != word[i + 1]) {
      result = result + word[i] + counter;
      counter = 0;
      i++;
    } else {
      counter++;
      i++;
    }
  }

  console.log(result);

  if (result.length < word.length)
    console.log(result)
  else
    console.log(word);
}

console.log(countWords())
&#13;
&#13;
&#13;

6 个答案:

答案 0 :(得分:2)

你可以使用Regex的强大功能,减少数组和条件三元组。

function compress(input) {
    var re = /(.)\1+|./gi;
    var match = input.match(re);
    var output = match.reduce(function (previousValue, currentValue) {
        return previousValue + (currentValue.charAt(0) + currentValue.length);
    }, "");
    output = (output.length < input.length) ? output : input;
    return output;
}
console.log(compress("aabcccccaaa"));

答案 1 :(得分:0)

你还有很多事情要做。

首先,您将循环索引递增两次。从i++声明中删除if

其次,您需要将计数器初始化为1而不是0。

此代码似乎有效。

<script>
function countWords()
    {
    var word = "aabcccccaaa";
    var result = "";
    var counter = 1;

    for(var i = 0; i <= word.length; i++) {
        if(word.substr(i, 1) === word.substr(i+1, 1)) {
            counter++;
        }
        else {
            result = result + word.substr(i, 1) + counter;
            counter = 1;
        }
    }

    console.log(result);

    if(result.length < word.length)
        console.log(result)
    else
        console.log(word);
}

countWords();
</script>

答案 2 :(得分:0)

    function countChars(word) {
        if (!word) {
            return word;
        }
        var arr = word.split('');
        var currentChar = arr[0];
        var count = 1;
        var compression = "";
        for (var i = 1; i < arr.length; i++) {
            if (arr[i] === currentChar) {
                count++;
            } else {
                compression += currentChar + count;
                currentChar = arr[i];
                count = 1;
            }
        }
        compression += currentChar + count;
        
        if (compression.length > word.length) {
            return word;
        }
        return compression;
    }
    
    console.log(countChars("aabcccccaaa"));

答案 3 :(得分:0)

可以使用reduce函数完成:

let word = 'aabcccccaaa';
let chars = word.split('');
let counter = 1;
let occur = 0;

let result = chars.reduce(function(prevValue, currValue, index, array) {
  let isLastChar = array.length - 1 === index;

  if (prevValue.substr(prevValue.length - 1) !== currValue) {
    occur = counter;
    counter = 1;
    prevValue = prevValue + occur + currValue;
    return isLastChar ? prevValue + 1 : prevValue
  } else {
    counter++;
    return isLastChar ? prevValue + counter : prevValue;
  }
});

答案 4 :(得分:0)

这是修复了这些错误的代码。

  1. counter0开始,因此计数将被关闭。
  2. 1)类似,每次都将counter设置回0而不是1
  3. 在for循环中递增i,以便跳过值。
  4. &#13;
    &#13;
    function countWords() {
    
      var word = "aabcccccaaa";
      var result = "";
      
      var counter = 1;
      for (var i = 0; i <= word.length; i++) {
        if (word[i] != word[i + 1]) {
          result = result + word[i] + counter;
          counter = 1;
        } else {
          counter++;
        }
      }
    
      return result.length < word.length ? result : word;
    
    }
    
    console.log(countWords());
    &#13;
    &#13;
    &#13;

答案 5 :(得分:0)

 var inputString = "Javascript",
      searchChar, count = 0;
    accurancesRepeat(inputString);

    function accurancesRepeat(inputString) {
      for (var i = 0; i < inputString.length; i++) {
        searchChar = inputString[i];
        for (var j = 0; j < inputString.length; j++) {
          if (searchChar == inputString[j]) {
            count += 1;
          }
        }

        console.log("Accorances of character " + searchChar + " is... " + count);
        count = 0;
      }
    }