Skip to content
Advertisement

How to encode string in javaScript

I am doing an exercise, the task is: I need to write a function which takes an argument a string and returns encoded string, for example: aaabbcccc -> should return 3a2b4c. I wrote the function, I created an object where I have all characters counted but I don’t know how to return value and key converted into the string. Here is my code

JavaScript

Advertisement

Answer

If your input string could contain duplicate chars, for example aabbbbaaa, you’d need to use an array of stats for each occurrence of the symbol

Also, object properties do not have order and might be “reorganized” by the runtime for performance reasons. For example, if your input string could contain numbers itself aaa111bbb

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement