Как можно сделать перенос строки на каждом n'ом символе? Подскажите пожалуйста.
var str="You can communicate not only with friends, but also get acquainted with the mass of new interesting people.";
for (var i = 0; i < str.length; i++) {
document.write(str[i])
}
В цикл добавляем ещё одну переменную с учётом числа символа:
var str="You can communicate not only with friends, but also get acquainted with the mass of new interesting people.";
var carry_option = {character_number: 4, carry_character: '<br>'}
for (var i = 0, n = 1; i < str.length; i++, n++){
document.write(str[i]);
// Если n = нашему числу, то переносим и обнуляем
if( n == carry_option.character_number ){
document.write( carry_option.carry_character );
n = 0;
};
};
Можно разбить строку на кусочки с помощью str.match(/.{1,N}/g)
var str="You can communicate not only with friends, but also get acquainted with the mass of new interesting people.";
var n = 8;
var r = new RegExp('.{1,'+n+'}', 'g')
var chunks = str.match(r)
//chunks.forEach(chunk => document.write(chunk+'<br />'));
document.write(chunks.join('<br />'))
Ну или можно просто воспользоваться оператором получения остатка от деления(%):
var str="You can communicate not only with friends, but also get acquainted with the mass of new interesting people.";
var n = 8;
for (var i = 0; i < str.length; i++) {
document.write(str[i]);
if ((i+1)%n == 0) document.write('<br/>');
}
str.match(/.{1,n}/g).join("\n")
Современные инструменты для криптотрейдинга: как технологии помогают принимать решения
Апостиль в Лос-Анджелесе без лишних нервов и бумажной волокиты
Основные этапы разработки сайта для стоматологической клиники
Продвижение своими сайтами как стратегия роста и независимости