How to wrap a line in a two-dimensional array if the number of elements is greater than a certain number?

  • 0
    You need to display a two-dimensional array with a different number of characters in each line, if the number of characters in a line is more than 20, transfer the remainder to a new line. Let's say

    [Hello me],
    [name, Ivan, Ivanovich, Ivanov]

    That the word "Ivanov" was already on a new line.
    JavaScript Anonymous, Jul 11, 2020

  • 1 Answers
  • 0
    It doesn't matter that the array is two-dimensional. In fact, you need to output a single hyphenated array of characters. You just need to solve this problem separately for each row of the two-dimensional array.

    The simplest solution is to print '\ n' in the character output loop if the current character is the 20th, 40th, etc. For example, you can check (i + 1)% 20 == 0 .

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!