問題描述
批量生成 兩個字母 + 三個數字 的隨機數
解決方案
function getRandomByStr(l = 3, s) { if (typeof s !== 'string') { return } var len = +l;
var chars = ''; while (len--) { chars += s[parseInt(Math.random() * s.length, 10)]; } return chars; } function getPlate(total = 9) { var en = 'QWERTYUIOPASDFGHJKLZXCVBNM'; var num = '1234567890'; var len = +total; while (len--) { console.log(`車牌號 ${total - len}:`, getRandomByStr(2, en) + getRandomByStr(3, num)); } } getPlate();