一、Math
查内置对象使用方法mdn:https://developer.mozilla.org/zh-CN
1)max/min PI ceil floor round random随机取两数之间整数 随机点名
console.log(Math.max(1,3,4,5,6))
console.log(Math.min(1,3,4,5,6))
console.log(Math.PI)
console.log(Math.ceil(1.33)) //向上取整
console.log(Math.floor(1.3)) //向下取整
console.log(Math.round(1.5)) //四舍五入 2
console.log(Math.round(-1.5)) //四舍五入 -1
console.log(Math.random()) //[0,1)
// 【1】取两数之间的随机整数
function getRandom(min,max){
return Math.floor(Math.random()*(max-min+1))+min
}
console.log(getRandom(1,12))
// 【2】随机点名
var names=['小明','小红','小狗','小猪','小事儿']
console.log(names[getRandom(0,names.length-1)])
2)猜数字
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>猜数字</title>
</head>
<script>
//取两数间随机整数
function getRandom(min,max){
return Math.floor(Math.random()*(max-min+1))+min
}
var num=getRandom(1,8) //设置一个随机数
while(true){
var yourNum= prompt('请猜1-8之间整数')
if(num<yourNum){
alert('猜大了哦')
}else if(num>yourNum){
alert('猜小了哦')
}else{
alert('猜对啦')
break //猜对退出循环
}
}
</script>
<body>
</body>
</html>