當前位置:成語大全網 - 新華字典 - 用 javascript 隨機生成中間四位帶星號的手機號碼的方法

用 javascript 隨機生成中間四位帶星號的手機號碼的方法

//?根據字典生成隨機序列

var?randomCode?=?function?(len,dict)?{

for?(var?i?=?0,rs?=?'';?i?<?len;?i++)

rs?+=?dict.charAt(Math.floor(Math.random()?*?100000000)?%?dict.length);

return?rs;

};

//?生成隨機手機號碼

var?randomPhoneNumber?=?function(){

//?第1位是1?第2,3位是3458?第4-7位是*?最後四位隨機

return?[1,randomCode(2,'3458'),'****',randomCode(4,'0123456789')].join('');

};

console.log(randomPhoneNumber());