Count repeated words using javascript












0














I am trying to solve this exercise but i a 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 e.g "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())












share|improve this question
























  • Not a question. Define your problem.
    – Fabian Klötzl
    Mar 3 '17 at 17:28










  • How isn't your code working? What part do you need help with?
    – qxz
    Mar 3 '17 at 17:28






  • 4




    You might find this helpful: stackoverflow.com/help/how-to-ask
    – let_the_coding_begin
    Mar 3 '17 at 17:29


















0














I am trying to solve this exercise but i a 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 e.g "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())












share|improve this question
























  • Not a question. Define your problem.
    – Fabian Klötzl
    Mar 3 '17 at 17:28










  • How isn't your code working? What part do you need help with?
    – qxz
    Mar 3 '17 at 17:28






  • 4




    You might find this helpful: stackoverflow.com/help/how-to-ask
    – let_the_coding_begin
    Mar 3 '17 at 17:29
















0












0








0







I am trying to solve this exercise but i a 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 e.g "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())












share|improve this question















I am trying to solve this exercise but i a 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 e.g "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())








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





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






javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 3 '17 at 18:48









Jonathan Portorreal

1,3762725




1,3762725










asked Mar 3 '17 at 17:25









Gilberto Langarica

175




175












  • Not a question. Define your problem.
    – Fabian Klötzl
    Mar 3 '17 at 17:28










  • How isn't your code working? What part do you need help with?
    – qxz
    Mar 3 '17 at 17:28






  • 4




    You might find this helpful: stackoverflow.com/help/how-to-ask
    – let_the_coding_begin
    Mar 3 '17 at 17:29




















  • Not a question. Define your problem.
    – Fabian Klötzl
    Mar 3 '17 at 17:28










  • How isn't your code working? What part do you need help with?
    – qxz
    Mar 3 '17 at 17:28






  • 4




    You might find this helpful: stackoverflow.com/help/how-to-ask
    – let_the_coding_begin
    Mar 3 '17 at 17:29


















Not a question. Define your problem.
– Fabian Klötzl
Mar 3 '17 at 17:28




Not a question. Define your problem.
– Fabian Klötzl
Mar 3 '17 at 17:28












How isn't your code working? What part do you need help with?
– qxz
Mar 3 '17 at 17:28




How isn't your code working? What part do you need help with?
– qxz
Mar 3 '17 at 17:28




4




4




You might find this helpful: stackoverflow.com/help/how-to-ask
– let_the_coding_begin
Mar 3 '17 at 17:29






You might find this helpful: stackoverflow.com/help/how-to-ask
– let_the_coding_begin
Mar 3 '17 at 17:29














6 Answers
6






active

oldest

votes


















2














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





share|improve this answer































    0














    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>





    share|improve this answer























    • I changed the logic from != to === just to make it more clear to me but I think the other way would work as well.
      – clarmond
      Mar 3 '17 at 17:53



















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








    share|improve this answer





























      0














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





      share|improve this answer































        0














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








        share|improve this answer































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








          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42585019%2fcount-repeated-words-using-javascript%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            6 Answers
            6






            active

            oldest

            votes








            6 Answers
            6






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














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





            share|improve this answer




























              2














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





              share|improve this answer


























                2












                2








                2






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





                share|improve this answer














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






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 3 '17 at 19:40









                Donald Duck

                3,960123958




                3,960123958










                answered Mar 3 '17 at 18:59









                Cleudson Cunha

                212




                212

























                    0














                    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>





                    share|improve this answer























                    • I changed the logic from != to === just to make it more clear to me but I think the other way would work as well.
                      – clarmond
                      Mar 3 '17 at 17:53
















                    0














                    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>





                    share|improve this answer























                    • I changed the logic from != to === just to make it more clear to me but I think the other way would work as well.
                      – clarmond
                      Mar 3 '17 at 17:53














                    0












                    0








                    0






                    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>





                    share|improve this answer














                    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>






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Mar 3 '17 at 17:52

























                    answered Mar 3 '17 at 17:44









                    clarmond

                    33217




                    33217












                    • I changed the logic from != to === just to make it more clear to me but I think the other way would work as well.
                      – clarmond
                      Mar 3 '17 at 17:53


















                    • I changed the logic from != to === just to make it more clear to me but I think the other way would work as well.
                      – clarmond
                      Mar 3 '17 at 17:53
















                    I changed the logic from != to === just to make it more clear to me but I think the other way would work as well.
                    – clarmond
                    Mar 3 '17 at 17:53




                    I changed the logic from != to === just to make it more clear to me but I think the other way would work as well.
                    – clarmond
                    Mar 3 '17 at 17:53











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








                    share|improve this answer


























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








                      share|improve this answer
























                        0












                        0








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








                        share|improve this answer















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








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





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






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Mar 3 '17 at 17:55









                        beta-developper

                        1,321814




                        1,321814























                            0














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





                            share|improve this answer




























                              0














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





                              share|improve this answer


























                                0












                                0








                                0






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





                                share|improve this answer














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






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Mar 3 '17 at 19:24

























                                answered Mar 3 '17 at 18:52









                                Zacol

                                785




                                785























                                    0














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








                                    share|improve this answer




























                                      0














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








                                      share|improve this answer


























                                        0












                                        0








                                        0






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








                                        share|improve this answer














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








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





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






                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Mar 3 '17 at 19:49

























                                        answered Mar 3 '17 at 19:19









                                        Jonathan Portorreal

                                        1,3762725




                                        1,3762725























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








                                            share|improve this answer




























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








                                              share|improve this answer


























                                                0












                                                0








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








                                                share|improve this answer

















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








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





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






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Nov 21 '18 at 10:54









                                                barbsan

                                                2,20811122




                                                2,20811122










                                                answered Nov 21 '18 at 10:32









                                                Regan Amburose

                                                32




                                                32






























                                                    draft saved

                                                    draft discarded




















































                                                    Thanks for contributing an answer to Stack Overflow!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.





                                                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                    Please pay close attention to the following guidance:


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function () {
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42585019%2fcount-repeated-words-using-javascript%23new-answer', 'question_page');
                                                    }
                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    Wiesbaden

                                                    Marschland

                                                    Dieringhausen