V
V
Volodymyr Palamar2021-11-14 20:21:19
JavaScript
Volodymyr Palamar, 2021-11-14 20:21:19

How to space rows evenly?

Hello, there are 2 lines,
one with source text , the other with invisible Unicode characters , some function is needed or tell me an algorithm to evenly "dissolve" the encoded text in the sourcelet a = "hello world" //lenght=10
let b = "........" //lenght=8

magicfunc(a,b) // => he.l.l.o. .w.o.r.ld
let c = "hi"
magicfunc(c,b) // => ..h....i..

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dollar, 2021-11-14
@GORNOSTAY25

I propose such a simple algorithm.
First, we find out which string is larger by comparing length1 and length2. What is more, in that we will dissolve.
For example, the path length1 >= length2 (example with "hello world"). Then we calculate how many characters to alternate: x = Math.round(length1/length2). Next, in the loop, we take x characters from the original string, then 1 character from the string with invisibles. And so on until the end, until the lines run out. In a loop, we glue these pieces into the resulting string.
If length1 < length2, then everything is the same, only we take 1 character from the original and x from the string with invisibles.
Of course, if you need to somehow smear it in a special way, then the algorithm will need to be improved a little, but the essence will be the same.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question