题解 | #字符串分隔#

字符串分隔

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);

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-15 17:32
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务