JS自学Day6-内置对象

目录

2.1内置对象

2.2内置对象Math

2.3生成任意随机数 

 生成随机数:

(案例)随机点名案例

(案例)随机点名案例-不允许重复显示

(案例)-猜数字游戏


2.1内置对象

JS内部提供的对象,包含各种属性和方法给开发者调用

思考:

ducument.write()

console.log()

2.2内置对象Math

Math对象是JS提供的一个“数学高手对象”

提供了一系列做数学运算的方法

常见的方法有:

random:向上取整

floor:向下取证

max:找最大数

min:找做小数

pow:幂运算

abs:绝对值

...

        console.log(Math.PI) //圆周率 π
        console(Math.random())  //返回随机数 一个浮点型伪随机数 [0,1)
        // 向上取整,返回整数
        console.log(Math.ceil(1.1))//ceil 2
        console.log(Math.ceil(1.5))//ceil 2
        console.log(Math.ceil(1.9))//ceil 2

        // 向下取整 返回的整数 floor
        console.log(Math.ceil(1,1))//floor 1
        console.log(Math.ceil(1,1))//floor 1
        console.log(Math.ceil(1,1))//floor 1

        // round 就近取整(负数 往大取整) 返回整数
        console.log(Math.round(1.1)) // round 1
        console.log(Math.round(1.5)) // round 2
        console.log(Math.round(1.9)) // round 2 

        console.log(Math.round(-1.1)) // round -1
        console.log(Math.round(-1.5)) // round -1
        console.log(Math.round(-1.9)) // round -2

        // 最大值和最小值
        console.log(Math.max(1,5,9,45))
        console.log(Math.min(1,5,9,45))

2.3生成任意随机数 

如何生成0-10的随机数?

Math.floor(Math.random()*(10 + 1))

如何生成5-10的随机数?

Math.floor(Math.random()*(5 + 1))+5

如何生成N-M之间的随机数

Math.floor(Math.raandom()*(M - N + 1))+ N

 生成随机数:

  // // 求得是N-M 之间的一个随机数公式
        // let random = Math.floor(Math.random()*(10 - 1 + 1)) 
        // + 1
        // console.log(random)

        // 封装一个一个随机函数 min到 max
        function getRandom(min,max){
            return Math.floor(Math.random()*(max - min + 1))+ min
        }
        let random = getRandom(1,10)
        console.log(random)  
        let random1 = getRandom(1,60)
        console.log(random1)

(案例)随机点名案例

需求:请把['赵云','黄忠','关羽','张飞','马超','刘备','曹操',]

生成一个随机数,作为数组索引号

  // 随机数
        function getRandom(min,max){
           return Math.floor(Math.random()*(max - min + 1))+ min
        }

        // 声明一个数组
        let arr = ['赵云','黄忠','关羽','张飞','马超','刘备','曹操']

        // 生成一个随机数
        let random = getRandom(0,arr.length - 1)
        document.write(arr[random])

(案例)随机点名案例-不允许重复显示

删除随机生成的下标,

    //不允许随机出现
     // 随机数
     function getRandom(min,max){
           return Math.floor(Math.random()*(max - min + 1))+ min
        }

        // 声明一个数组
        let arr = ['赵云','黄忠','关羽','张飞','马超','刘备','曹操']

        // 生成一个随机数
        let random = getRandom(0,arr.length - 1)
        document.write(arr[random])
        // 之后删除这个人的名字
        arr.splice(random,1)
        console.log(arr)

(案例)-猜数字游戏

需求:程序随机生成1~10之间的一个数字,用户输入一个数字

1:大于该数,提示,数字猜大了,继续猜

2:小于该数,提示,数字猜小了,继续猜

3:猜对了,提示,猜对了,程序结束

分析:

1.利用随机数生成一个数字

2.需要一直猜,需要不断循环

3.因为条件是结果猜对了,就是判断条件退出,用while循环合适

4.内部判断可以用多分支语句

    //随机数
        function getRandom(min,max){
            return Math.floor(Math.random()*(max - min + 1)
            )+ min
        }

        // 生成1~10之间的随机数
        let random = getRandom(1,10)
        console.log(random)
        // 用户输入 不断弹窗直到输入正确
        while(true){
            let num= +prompt('请您输入一个数字:')
            // 如果输入>随机数 提示 猜大了
            if(random < num){
                alert('猜大了')
            }else if(random > num){
                alert('猜小了')
            }else{
                alert('猜对了')
                break
            }

        }

你可能感兴趣的:(JavaScript,javascript,前端,html)