简体   繁体   中英

Count repeated words using javascript

I am trying to solve this exercise but ia little bit stuck, this is what i have right now, i am trying to iterate over the string incrementing the index plus one per word

"Write a function to perform basic string compression using the counts of repeated characters eg "aabcccccaaa" would become "a2b1c5a3", if the compressed string would not become smaller than the original, just print the original"

 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()) 

You can use the power of Regex, reduce in array and conditional ternary.

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"));

You have a couple of things going on.

First, you are incrementing your loop index twice. Remove the i++ from your if statement.

Secondly, you need to initialize your counter to 1 not 0.

This code seems to work.

<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>

  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")); 

It could be done with reduce function:

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;
  }
});

Here's your code with these bugs fixed.

  1. counter starting at 0 so the count would be off by one.
  2. similarly to 1) , setting counter back to 0 each time instead of 1 .
  3. incrementing i inside the for loop so values were being skipped.

 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()); 

  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; } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM