用随机的小写字母填充字符串以填充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 婆婆高寿祝福语大全简短