用随机的小写字母填充字符串以填充JavaScript的长度
我们需要编写一个带有两个参数的函数,第一个是字符串,第二个是数字。字符串的长度始终小于或等于数字。我们必须在字符串的末尾插入一些随机的小写字母,以使其长度与数字完全相等,并且我们必须返回新的字符串。
示例
让我们为该函数编写代码-
const padString = (str, len) => {
if(str.length < len){
const random = Math.floor(Math.random() * 26);
const randomAlpha = String.fromCharCode(97 + random);
return padString(str + randomAlpha, len);
};
return str;
};
console.log(padString('abc', 10));
console.log(padString('QWERTY', 10));
console.log(padString('HELLO', 30));
console.log(padString('foo', 10));输出结果
控制台中的输出-
abckoniucl QWERTYcwaf HELLOdnulywbogqhypgmylqlvmckhg Foofhfnhon
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短