zoukankan      html  css  js  c++  java
  • 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);
    
  • 相关阅读:
    pat00-自测5. Shuffling Machine (20)
    Spiral Matrix
    Search in Rotated Sorted Array II
    Search in Rotated Sorted Array
    Best Time to Buy and Sell Stock II
    4Sum
    3Sum Closest
    3Sum
    MySQL存储过程、函数和游标
    Word Ladder
  • 原文地址:https://www.cnblogs.com/zhuxiaopeng/p/6805625.html
Copyright © 2011-2022 走看看