用随机的小写字母填充字符串以填充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 生日广州男生祝福语简短