0

function countFig(str) {
  let arr = str.split('');
  let newArr = [];
  let a = arr[0];
  let sum = 0;
  for (let i = 0; i < arr.length; ++i) {
    if ((i = arr.indexOf(a, i)) === -1) break;
    newArr.push(i);
  }
  return newArr
}
console.log(countFig('dddmmmmccccaaamm'))

I have stucked with a simple task and don't understand how to make the decision about it. There is one condition - do not use regular expressions. The task is to write a function that counts similar elements of a string so if we have dddmmmmccccaaamm, the function returns d3m4c4a3m2. Firstly I thought about converting string to array and using filter method but it is the wrong way. Now I think to find the first element of the array and compare it with each element in the loop, but don't know what to do next.

Victor
  • 33
  • 7
  • 1
    Show us the code you have so far. – Niet the Dark Absol Nov 16 '21 at 14:35
  • 1
    Please see [ask]. You need to show an attempt. – isherwood Nov 16 '21 at 14:35
  • Loop over the individual characters, and use them as key names in an object. If `obj['d']` is already set, then increase the value of that by one, if not, add it to the object, with value `1`. Then you have an object that contains all the characters and their count at the end. – CBroe Nov 16 '21 at 14:38
  • I ve just posted my attempt – Victor Nov 16 '21 at 14:38
  • 1
    @RandyCasburn You are pointing at a question that has 39 answers, not a specific answer. It's hard to tell which one you think answers this question. The OP specifically said no regex but the accepted answer uses regex. I also can't find anything that outputs `d3m4c4a3m2` – Ruan Mendes Nov 16 '21 at 14:42
  • 1
    @RandyCasburn this answer doesn't match my question – Victor Nov 16 '21 at 14:43
  • 1
    @Victor Your attempt isn't really trying anything? It's hard to tell how you expect that code to do anything, it's never returning a value. The way the question is written, we can only write a solution for you. That is not the purpose of StackOverflow, we want to help you understand why your current approach doesn't work. Here's a tip: keep track of the last character in a loop and update a counter every time the character is the same, once the character changes, reset the counter and update the last character – Ruan Mendes Nov 16 '21 at 14:46
  • 2
    To whomever voted to close this as a duplicate, that question does not attempt to output something like `d3m4c4a3m2` – Ruan Mendes Nov 16 '21 at 14:52
  • @Victor I created [this fiddle to get you started](https://jsfiddle.net/mendesjuan/w6fs0y53/1/). Try it out and update this question if you can't figure it out. – Ruan Mendes Nov 16 '21 at 15:52
  • @JuanMendes thanks, I'm trying to figure it now – Victor Nov 16 '21 at 16:02
  • @JuanMendes could you write your solution in fiddle, I still don't know how to make it – Victor Nov 16 '21 at 16:59
  • @Victor Like I mentioned before, we want to help you learn, we don't want to solve problems for you and potentially keep you from a learning experience. Update the question to explain what you have tried and how it doesn't behave as expected. See https://stackoverflow.com/help/how-to-ask – Ruan Mendes Nov 16 '21 at 19:10
  • @Victor I did a [little bit more for you to guide you](https://jsfiddle.net/mendesjuan/w6fs0y53/4/). Please learn how to ask questions that encourage others to respond, I have been trying to guide you but you are not showing any signs of trying to do what I am asking you. Notice that it's intentionally buggy so you can solve it yourself. – Ruan Mendes Nov 16 '21 at 19:14

0 Answers0