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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM