Talk is cheap. Show me your code

JavaScript 随机生成车牌号

其实仅仅是因为想不出车牌号,干脆写一个函数随机生成算了...

 

问题描述

批量生成 两个字母 + 三个数字 的随机数

 

解决方案

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();

 

posted @ 2020-06-08 10:35  Wise.Wrong  阅读(2690)  评论(2编辑  收藏  举报