1

I need help how to solve this logic in my JS code, for input - output flow same as I draw on the paper

Thanks in advance.

an example of the logic flow that I drew

Dale K
  • 25,246
  • 15
  • 42
  • 71
Jhony Do
  • 73
  • 2
  • 8
  • what is your expected output? – Layhout Dec 10 '22 at 04:52
  • have you try anything? – Layhout Dec 10 '22 at 04:58
  • my expected output is from TOP - Break 5 will appear in the input component :) – Jhony Do Dec 10 '22 at 04:58
  • i fail to understand your expected output. what you have on that paper is code work flow. an output is like an array, an object, string or number. – Layhout Dec 10 '22 at 05:02
  • are the duplicates consecutive always as given? – Amila Senadheera Dec 10 '22 at 05:03
  • do you want your code to output something like this `{"123": 5, "66": 1, "55": 1, "32": 2}`? – Layhout Dec 10 '22 at 05:05
  • useful docs you should read: [split string method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split), [count number of occurrences](https://stackoverflow.com/questions/19480916/count-number-of-occurrences-for-each-char-in-a-string) – Layhout Dec 10 '22 at 05:08
  • As per the question guide, please do not post images of code, data, error messages, etc. - copy or type the text into the question. Please reserve the use of images for diagrams or demonstrating rendering bugs, things that are impossible to describe accurately via text. – Dale K Dec 10 '22 at 05:09

1 Answers1

0

Try like this:

const string = "123*123*123*123*123*66*55*32*32"

const arr = string.split("*")

const output = arr.reduce((prev, curr) => {
  prev[curr] = (prev[curr]?? 0) + 1
  return prev 
}, {})

console.log(output)

Using Array.prototype.reduce()

Amila Senadheera
  • 12,229
  • 15
  • 27
  • 43