我想要一个带3个参数的函数: 句子(字符串), maxCharLen = 20(数字), separator(string)
这是我的例子: 是否可以改进此代码?
function breakSentence(sentence) {
var newstring,
maxCharLen = 20,
separator = ',';
for(var i=0; i < sentence.length; i++) {
newstring += sentence.charAt(i);
if ((i % maxCharLen) === 0) {
newstring += separator;
}
}
}
答案 0 :(得分:1)
粗略的例子......
function breakSentence(sentence, maxCharLen, separator) {
var newstring = "";
for(i=0; i < sentence.length; i++) {
newstring += sentence.charAt(i);
if (((i % maxCharLen) == 0) & (i >= maxCharLen)) {
newstring += separator;
}
}
}