Java Script学习之旅——第六天

今日只学了JS视频,js基础视频已完成,明天开始下一部分。

                                                              取整、随机数:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    console.log(Math.PI)//Π
    //1.向上取整ceil
    console.log(Math.ceil(1.1))//2
    console.log(Math.ceil(1.5))
    console.log(Math.ceil(1.9))
    //2.向下取整floor
    console.log(Math.floor(1.1))//1
    console.log(Math.floor(1.5))
    console.log(Math.floor(1.9))
    //3.四舍五入round
    console.log(Math.round(1.1))//1
    console.log(Math.round(1.5))//2
    console.log(Math.round(1.7))//2
    console.log(Math.round(-1.1))//-1
    console.log(Math.round(-1.5))//-1——取大
    console.log(Math.round(-1.9))//-2
    //4.最大、最小、绝对值
    console.log(Math.max(1, 2, 3, 4, 5))
    console.log(Math.min(1, 2, 3, 4, 5))
    console.log(Math.abs(-1))
    //5.随机数
    console.log(Math.floor(Math.random() * 11))//0~11,左闭右开
    let arr = ['red', 'green', 'blue']
    let random = Math.floor(Math.random() * arr.length)
    console.log(arr[random])
    //6.N~M之间的随机数
    function getRandom(N, M) {
      return Math.floor(Math.random() * (M - N + 1)) + N
    }
    console.log(getRandom(2, 9))
    //案例
    /* let arr1 = ['张飞', '关羽', '赵云', '黄忠', '马超', '刘备', '曹操']
      let random1 = Math.floor(Math.random() * arr1.length)
      console.log(arr1[random1])*/
    //案例改进——不重复
    let arr1 = ['张飞', '关羽', '赵云', '黄忠', '马超', '刘备', '曹操']
    let random1 = Math.floor(Math.random() * arr1.length)
    document.write(arr1[random1])
    arr1.splice(random1, 1)
    console.log(arr1)
  </script>
</body>

</html>

                                                         猜数字、 随机颜色: 

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    /* function getRandom(N, M) {
       return Math.floor(Math.random() * (M - N + 1)) + N
     }
     let random = getRandom(1, 10)
     console.log(random)
     while (true) {
       let num = +prompt('请输入您猜的数字:')
       if (num > random) {
         alert('您输入的数字太大了!')
       }
       else if (num < random) {
         alert('您输入的数字太小了!')
       }
       else {
         alert('你猜对啦!')
         break
       }
     }*/
    //设定猜的次数
    /*function getRandom(N, M) {
      return Math.floor(Math.random() * (M - N + 1)) + N
    }
    let random = getRandom(1, 10)
    let flag = true
    for (i = 1; i <= 3; i++) {
      let num = +prompt('请输入您猜的数字:')
      if (num > random) {
        alert('您输入的数字太大了!')
      }
      else if (num < random) {
        alert('您输入的数字太小了!')
      }
      else {
        flag = false
        alert('你猜对啦!')
        break
      }
    }
    if (flag) {
      alert('次数已用完')
    }*/
    //随机颜色案例
    function getRandomColor(flag = true) {
      if (flag) {
        let str = '#'
        let arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
        for (let i = 1; i <= 6; i++) {
          let random = Math.floor(Math.random() * arr.length)
          str = str + arr[random]
        }
        return str
      } else {
        let r = Math.floor(Math.random() * 256)
        let g = Math.floor(Math.random() * 256)
        let b = Math.floor(Math.random() * 256)
        return `rgb(${r},${g},${b})`
      }
    }
    //调用函数
    console.log(getRandomColor(false))
    console.log(getRandomColor(true))
    console.log(getRandomColor())
  </script>
</body>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值