JS算法练习二

JS算法练习

1、生成4位的随机验证码,可取大小写字母和数字 ?

var validateCode = "",
/*--存放生成好的验证码字符串--*/
	count = 0;
/*--已生成的验证码位数--*/
while (count < 4) {
	var rand = Math.floor(Math.random()*74+48)
	/*--产生 48 ~ 122 之间的随机数字(在Unicode中数字及大小写字母的范围)
	  先产生 0 ~ 74 之间的随机数字,再加上 48--*/
	if( rand >= 48 && rand<=57 || rand >= 65 && rand <= 90 || rand >= 97 && rand <= 122 ){
	/*--判断该随机数字是否在 48~57(0-9)  65~90(A-Z)  97~122(a-z) 的范围内--*/
		count++;
	/*--生成一位随机验证码--*/
		validateCode += String.fromCharCode(rand)
	/*--将生成的当前位验证码连接到完整字符串变量中--
		静态 String.fromCharCode() 方法返回使用指定的Unicode值序列创建的字符串*/
	}
}
console.log(validateCode);
posted @ 2017-05-04 10:23  脱缰的小毛驴  阅读(137)  评论(0编辑  收藏  举报