main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function multiline(str, config = {}) {  
  const defaultConfig = {
    width: 10,
    newLine: '\n',
    indent: ''
  };
  const safeConfig = {
    ...defaultConfig,
    ...config
  };
  let result = '';
  for (let index = 0; index < str.length; index++) {
    const char = str[index];
    if (index % safeConfig.width === 0) {
      result += safeConfig.indent + char;
    } else if ((index + 1) % safeConfig.width === 0
            && index < str.length - 1 ) {
      result += char + safeConfig.newLine;        
    } else {
      result += char;
    }
  }
  return result;
}

console.log(multiline('Hello World!'));  

console.log(multiline('Hello World!', { width: 6 }));  

console.log(multiline('Hello World!', { width: 6, newLine: '*' }));  

console.log(multiline('Hello World!', { width: 6, newLine: '*', indent: '_' })); 
Native Browser JavaScript