Count how many times each word in a string - Help

Hi everyone

I have been trying my hardest this whole day to solve this problem but I’m stuck and don’t want to find the answer on google.

I know that there will be easier and simpler solutions but I wanted to stick with the way I wanted to solve.
Please help find and correct my mistakes in this code:

function reachZeroPosition(arrTemp, arrOrig, i) {
  for (var k = 0; k < arrTemp.length; k++) {
    if (arrTemp.indexOf(arrOrig[i]) !== 0) {
      var a = arrTemp.shift();
  return arrTemp;

function countWords(str) {
  var words = str.split(" ");
  var temp = [];
  var result = [];
  var fResult = [];
  var found;
  var count = 0;

  for (var j = 0; j < words.length; j++) {
    temp = words.slice();
    found = temp.indexOf(words[j]);

    for (var i = 0; i < words.length; i++) {
      if (found !== -1) {
        temp = reachZeroPosition(temp, words, j);
        if (count === 0) {
          result[j] = temp.shift();
        result[j + 1] = ++count;
      } else {
      found = temp.indexOf(words[j]);

    count = 0;
  return fResult;

countWords("ask a bunch get a bunch");

Here’s an explanation of what I have done:

1- Converted String to array of words.
2- Copied the array to a temp array.
3- This temp will be shortened by the function above to make the desired word at position 0.
4- I keep track of Count and store both word and count in an array.
5- temp will be updated from words for each word.
6- words is never changed that’s why I used slice to copy.
6- I push the result to a final array.

There’s an issue in the logic, I tried debugging but I got nowhere

Please help

What do you think? @KoniKodes @lasjorg @shimphillip

You did not tell us the actual challenge instructions, but I assume based on your code, the final array is supposed to be an array of sub arrays with each sub array containing the word as the first element and the count of the word in the second element?

countWords("ask a bunch get a bunch"); 

The above call should return [ ['ask' 1], ['a', 2], ['bunch', 2], ['get', 1] ]?

1 Like

Yeah that’s exactly what I have been doing

Do the sub arrays need to appear in the same order the words appeared in the str? For example, would the following be acceptable?

 [  ['a', 2], ['ask', 1], ['bunch', 2], ['get', 1] ]
1 Like

It’s okay but I tried to maintain the same order of words in the final result

This challenge can be solved in 2-4 lines of code if the sub array order does not matter.

But as a beginner I could not reach such a short and optimized answer unfortunately
Is there any way to debug my code and get it to work?

Why don’t you first try to write out an algorithm in plain language which describes the steps to solve the problem. You have written steps of a summary of what you think your code does, but you need to write out the logical decision points (typically where you would have some conditional statement like an if or if/else statement). Based on what you have written so far in your steps, you are not describing that logic, which is obviously why your solution does not work.

For example, your step #4 above states:

I keep track of Count and store both word and count in an array.

Your algorithm would detail the logic involved with how you do this.

The same goes for steps #5:

temp will be updated from words for each word.

Describe in words the logic of how you want to do this.

Until you get the algorithm correct, it is pointless for anyone to “fix” your code. You way need to completely change your code, once you realize what steps are needed in your algorithm.

1 Like

Thanks for your response
I’ll try to come up with an algorithm and see if I could solve this basic problem or not


I think the problem is in the scope of the functions.
Where do you store arrTemp and arrOrig? and How do you get them into countWords?