V
V
vasyok2282021-09-30 22:50:29
JavaScript
vasyok228, 2021-09-30 22:50:29

How can I make even tabs in a line?

How can I implement even indentation depending on the lines?

There is a line, I need to somehow count the number of characters in all lines, and where there are more characters, from there attach tabs, 4 spaces, spaces are needed where it is \t.
The line itself:

let text = 
  '\n\
  Склад:\t\t50 из 100\n\
  Заказано:\t\t%s\n\
  \tВведите сколько желаете заказать:'


As a result, you need:
'\n\
Склад:           50 из 100\n\  // тут получается 11 пробелов, так как тоже ст. 2 длиннее
Заказано:        5\n\          // ст2. тут получается 8 пробелов, самая длинная строка
Остаток:     50\n\             // тут получается 5 пробелов, так как ст.2 длине на один сим. 
    Введите сколько желаете заказать:  // Тут только 4, так как перед строкой нет символов '

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dollar, 2021-10-01
@vasyok228

How can I make even tabs in a line?

This can be done in a way that is usually called "on the forehead" . That is, in fact, just take it and do it, because there are no difficulties even for a novice programmer. The problem does not exist as such.
Only a lazy person who wants to shift all the work onto the shoulders of another person, or a person ignorant of programming, can not cope. Judging by the fact that you provide the code, you still have something to do with programming, trying to do something yourself. Therefore, this is not the second, but the first option - laziness.
Very sorry.
With an independent solution, when the task seems too complicated, it needs to be divided into subtasks (as if stages), each of which is already within the power, and if not, then further split into even smaller subtasks. This process is called decomposition - this is one of the key skills of programmers and not only them.
For example, you have a subtask:
I need to somehow count the number of characters in all strings

Great, because this is a separate subtask that can be isolated and simply solved, abstracting from connections with other subtasks (after all, this will still have time):
text.split('\n').forEach(
  (s,i)=>console.log('Символов в строка №'+(i+1)+': '+s.length)
);

You probably think that this is not quite what you need. That's right, because this is just an example of how easy it is to solve a single simple subproblem. And given all the connections, this solution will have to be slightly modified to suit your Wishlist, but this is already you yourself somehow.
I offer you the following algorithm for your further actions:
  • If you understand the code above and can easily write a similar one, then, as I answered above, just solve your problem head-on. Break a complex task into subtasks. Teaching you this skill is beyond the scope of answering your question, but you can start with Wikipedia .
  • If you hardly understood the code above or did not understand anything at all, but would like to understand, then tighten up your knowledge of the language itself. Without such basic concepts as "array" or "loop", it is almost impossible to program at all. Explaining the basics is beyond the scope of answering your question, but you can start by reading the tutorial .
  • If you don’t want to understand anything, but just want to be offered a ready-made solution and that it just works, then tighten up your ability to formulate exactly what you want. Your current question is formulated chaotically, in the style of "so that I have everything, and I don't have anything for it." With this approach, you will have to wait too long for a lvl 80 telepath. It will be better to explain the details beautifully, briefly and clearly so that any specialist can help without clarifying questions. Alas, explaining the principles of how to ask questions is again beyond the scope of answering your original question, but you can start by reading the rules of this resource . Although they will not teach you to succinctly formulate thoughts. But you have to start somewhere, right?

A
Alexander Karabanov, 2021-09-30
@karabanov

I don’t know if there is a prinf function in JS or its analogue, but it is what you need.
Here is something similar https://stackoverflow.com/questions/17224130/forma...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question