用随机的小写字母填充字符串以填充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 教师闺蜜祝福语简短