题解 | #字符串分隔#
字符串分隔
http://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
写一个函数,实现每8个字符分割一次
1.如果字符串为空,直接输出 2.如果字符串小于8,输出并且需要将用0补齐8位 3.如果字符串大于8,输出前8位并且继续调用分割函数
function splitStr(str) {
if (str.length === 0) {
console.log(str)
} else if (str.length < 8) {
console.log(str + '0'.repeat(8 - str.length))
} else {
console.log(str.slice(0, 8));
splitStr(str.slice(8))
}
}
const str = readline();
splitStr(str);