javascript:常用API学习Math.random, toString,slice(),substr(),Math.ceil()

1.获得随机数:Math.random()

Math.random();

console.log(Math.random());//随机数在范围[0,1),是float类型:
//Math.random()*100)       范围[0,100)

获得小数

如何随机获得整数?

Math.ceil(); //把一个小数变整数,都进位无论:是0.6进1,0.1进1
Math.ceil(1.3); //得到整数:2

console.log(Math.ceil(Math.random()*100));/

2.如何转进制:十进制转二进制?: 变量名.toString(进制数)

var i=3;
console.log(i.toString(2));// 输出了:11  把3转换成2进制

console.log(i.toString(36));  //输出了3  36进制

console.log(i.toString(10));//输出3;  把3转换成10进制

3. 36进制:能把一个小数转换成:由数字+字母组成的较长的字符串了:

 var i=0.567;
var t=0.4596438;
console.log(i.toString(36));  / 36进制:得到0.kety9sifjvb
console.log(t.toString(36));   /  得到0.gjp52wowq2f

36进制:整数 变成整数(原来的)

var p=3;
console.log(p.toString(36));    //输出:3

4.从数组或字符串截取长度?:构成新的数组或字符串:slice()

slice(begin,end);
slice(0,2);//从第一个数组元素或第一个字符串第一个
//到第二个截取
slice(0) // 只截取第一个数组元素

console.log("aswddff".slice(0));//得到:aswddff//默认第二个end=最后一个元素
console.log("aswddff".slice(0,2));//得到:asw

console.log("aswddff".slice(-1));//得到:f//-1是最后一个位置

5.substr()截取字符串;

substr(1,2)1代表起始位置,2,表示截取长度,返回结果为截取的结果;
substr(0); //默认:从起始位置截取到最后

console.log("aswddff".substr(0));//得到:aweddff
console.log("aswddff".substr(2));//得到   wddff
console.log("aswddff".substr(0,1));//得到:a

6.验证码制作:(在b站看到一个视频,跟着打的)

:快捷键:.box>.box1+input+p+button //敲回车:快速生成
页面一半放执行窗口,一半放写代码:加快写代码速度

input.value==box1.innerHTML: 用作:判断语句

.box{
    width: 200px;
margin: 15% auto;

}


.box1{

width: 170px;
height: 50px;
background-color: pink;
text-align: center;

}

.eee{


    color:red;
}
.ert{

    color: black;
}


</style>
<body>
    
   <div class="box">
       <div class="box1"></div>
       <input type="text"><br>
       <p></p>
       <button>验证</button>
   </div>




<script>


var button=document.querySelector('button');
 var box1=document.querySelector('.box1');
 var input=document.querySelector('input');
var p=document.querySelector('p');




box1.innerHTML=Math.random().toString(36).slice(0-4)
box1.onclick=function(){


    box1.innerHTML=Math.random().toString(36).slice(1-5)



}

//Math.random().toString(36).substr(2).slice(0-4)
//Math.random()随机产生一个数

//toString(36)
//toString()方法可以根据所传递的参数把数值转换为对应进制的数字字符串。参数范围为 2~36 之间的任意整数。
//资料

//15975人阅读
//slice()通过索引位置获取新的数组,该方法不会修改原数组,只是返回一个新的子数组。
//
//
button.onclick=function(){

console.log(box1.innerHTML);



if(input.value==box1.innerHTML){

p.innerHTML="输入正确!";
p.className="ert";
}

else{
    p.innerHTML="输入错误!";
    p.className="eee";
}


}

//.toString(36).substr(2).Math.random()

//substr(1,2)js中substr的用法;substr(1,2) :截取字符串;(1,2)1代表起始位置,2,表示截取长度,返回结果为截取的结果;


</script>



效果图:

在这里插入图片描述

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半浮名

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值