码迷,mamicode.com
首页 > Web开发 > 详细

js实现的随机输出大小写字母代码

时间:2016-02-12 16:19:53      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

js实现的随机输出大小写字母代码:
本章节介绍一下如何使用javascript实现输出随机的大写字母或者小写字母,希望能够给大家带来或多或少的帮助。
代码如下:

 

function getCharacter(flag){ 
  var character=""; 
  if(flag==="lower"){ 
    character = String.fromCharCode(Math.floor(Math.random()*26)+"a".charCodeAt(0)); 
  } 
  if(flag==="upper"){ 
    character = String.fromCharCode(Math.floor(Math.random()*26)+"A".charCodeAt(0)); 
  } 
  return character; 
} 
function getUpperCharacter(){ 
  return getCharacter("upper");; 
}
function getLowerCharacter(){ 
  return getCharacter("lower");; 
} 
console.log(getUpperCharacter());
console.log(getLowerCharacter());

 

以上代码实现了我们的要求,能够随机输出大写字母或者些小字母,原理非常的简单,就是利用了大写字母或者小写字母Unicode码的区间来实现的,更多内容这里就不多介绍了,可以参阅相关阅读。
相关阅读:
1.fromCharCode()函数可以参阅js的fromCharCode()函数用法简单介绍一章节。
2.Math.floor()函数可以参阅javascript的Math.floor()方法一章节。  
3.Math.random()函数可以参阅js的随机函数Math.random()一章节。
4.charCodeAt()函数可以参阅javascript的String对象的charCodeAt()方法一章节。

原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=12257

更多内容可以参阅:http://www.softwhy.com/javascript/

 

js实现的随机输出大小写字母代码

标签:

原文地址:http://www.cnblogs.com/zhengzebiaodashi/p/5187089.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!