Skip to content
Advertisement

Adding new line after every 15 characters in JavaScript

I have used following method to do the changes, but I’m getting additional spaces after adding the new line. I used trim(), but it makes the value meaningless.

JavaScript

Advertisement

Answer

Are you talking about the space on the last line of the output Throt ? There’s actually no more data there, but if you want your input string to repeat so that it fills in the rest of that space, you’ll need to repeat it so that it fills it as much as possible.

The base string of ER Throttle Position (with ending space) is 21 characters long. For a line length of 15, repeating the base string 5 times would result in 7 lines of repeated text that fills in the full width (unless you’re counting the final space):

JavaScript
JavaScript
JavaScript

Changes made in my code:

  • Added two parameters to generalize the function to add new lines every position characters
  • Added a line to display spaces using underscores _ (optional)
  • Commented the assignment of result to columnValue before returning columnValue
  • Returned result instead
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement