在JavaScript中生成具有特定长度的随机字符串
我们需要编写一个JavaScript函数,该函数将数字作为一个唯一的参数。然后,该函数应返回由参数指定长度的随机生成的字符串。
用于生成字符串的字符集应仅包含大写和小写字母(不能包含空格,标点符号或数字)。
示例
为此的代码将是-
const num = 13;
const randomString = (len = 1) => {
const charSet =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
let randomString = '';
for (let i = 0; i < len; i++) {
let randomPoz = Math.floor(Math.random() * charSet.length);
randomString += charSet.substring(randomPoz,randomPoz+1);
};
return randomString;
};
console.log(randomString(num));输出结果
控制台中的输出将是-
EqprjcudAhmVg
每次运行的输出可能会有所不同。
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语