JS-day06-2

目录

01-Number

02-Date

03-时间戳

04-Math

05-猜数字

06-全局对象

07-数据类型

08-instanceof

09-总结


01-Number

<!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>
        var num = 3.1415926;
        var num2 = new Number(20);

        console.log(num.toFixed(2));    // 字符串

        // 进制转换 toString()  2 8 10 16
        var num3 = 23;
        console.log(num3.toString(2));  // 1010  0*2^0 + 1*2^1 + 0*2^2 + 1*2^3

        var num4 = 0b1010;  // 2进制
        var num5 = 0O10;    // 8进制
        var num6 = 0x10;    // 16进制

        console.log(num5.toString(10));

        console.log(num3.toLocaleString("ar-EG"));
    </script>
</body>

</html>

02-Date

<!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>
        // 日期对象
        var date = new Date();
        console.log(date);      // 当前时间
        console.dir(date);

        // 获取目标时间
        var fulter = new Date("2024-9-17 12:30:30");
        console.log(fulter);

        console.log("-------------------------");

        // 当前时间:2024年7月24日 周三 10:51:00
        // 获取年 getFullYear()
        var year = date.getFullYear();
        console.log(year);

        // 获取月 getMonth()+1
        var month = date.getMonth() + 1;
        console.log(month);

        // 获取日 getDate()
        var day = date.getDate();
        console.log(day);

        // 获取星期 getDay()
        var week = date.getDay();   // 3
        console.log(week);
        var weeks = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
        console.log(weeks[week]);

        // 获取时
        var hours = date.getHours();
        console.log(hours);

        // 获取分
        var minutes = date.getMinutes();
        console.log(minutes);

        // 获取秒
        var seconds = date.getSeconds();
        console.log(seconds);

        // 写入到文档
        document.write("当前时间为:" + year + "年" + month + "月"
         + day + "日 " + weeks[week] + " " + trueTime(hours) +
          ":" + trueTime(minutes) + ":" + trueTime(seconds));

        // 时间格式化函数
        function trueTime(time){
            if(time < 10){
                time = "0" + time;
            }

            return time;
        }
    </script>
</body>

</html>

03-时间戳

<!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>
        // 实例化日期对象
        var date = new Date();

        // 毫秒数
        console.log(date.valueOf());        // 1721801108137
        console.log(date.getTime());

        // 倒计时

        // 中秋节放假倒计时
        var fulter = new Date("2024/9/17");
        console.log(fulter.valueOf());      // 1726502400000

        var time = fulter.valueOf() - date.valueOf();

        // 1s = 1000ms
        var s = parseInt(time / 1000 % 60);
        console.log(s);

        var min = parseInt(time / 1000 / 60 % 60);
        console.log(min);

        var h = parseInt(time / 1000 / 60 / 60 % 24);
        console.log(h);

        var d = parseInt(time / 1000 / 60 / 60 / 24);
        console.log(d);

        document.write("距离中秋假期还剩:" + d + "天" + h + "小时" + min + "分钟" + s + "秒");
    </script>
</body>

</html>

04-Math

<!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>
        // Math.PI  内置对象

        // 圆周率
        console.log(Math.PI);

        // 最值
        console.log(Math.max(1, 2, 3, 4, 51, 6, 7, 18, 9));
        console.log(Math.min(1, 2, 3, 4, 5, 6, 7, 8, 9));

        // 绝对值
        console.log(Math.abs(-10));

        // 次幂
        console.log(Math.pow(2, 3));
        console.log(2 ** 3);

        // 向下取整
        console.log(Math.floor(-3.14));
        // 向上取整
        console.log(Math.ceil(3.14));

        // 四舍五入
        console.log(Math.round(3.64));

        console.log("---------------");
        // 取随机数     [0,1)   [0,11)
        console.log(Math.floor(Math.random() * 11));

        // Math.floor(Math.random() * (max - min + 1) + min);
        console.log(Math.floor(Math.random() * (100 - 50 + 1) + 50));
    </script>
</body>

</html>

05-猜数字

<!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>
        var num = Math.floor(Math.random() * 100 + 1);
        console.log(num);

        var user = prompt("请输入数字");

        // 假设一次就能猜中
        var flag = false;

        do {
            if (user > num) {
                user = prompt("猜大了,请重新输入");
                // 没猜对接着猜,循环需要继续
                flag = true;
            } else if (user < num) {
                user = prompt("猜小了,请重新输入");
                flag = true;
            } else {
                alert("猜对了!");
                flag = false;
            }
        } while (flag);

        // while (true) {
        //     if (user > num) {
        //         user = prompt("猜大了,请重新输入");

        //     } else if (user < num) {
        //         user = prompt("猜小了,请重新输入");

        //     } else {
        //         alert("猜对了!");
        //         break;
        //     }
        // }
    </script>
</body>

</html>

06-全局对象

<!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>
        // window
        // window.alert();
        // alert();

        // 掌握已学知识即可
        console.log("1+2");
        console.log(eval("1+2")); // 1+2
    </script>
</body>

</html>

07-数据类型

<!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>
        // 数据类型
        // 1.基本数据类型
        // number string boolean undefined null
        // 2.引用数据类型
        // object function Array Math Date

        var arr = [1, 2, 3, 4, 5];
        console.log(arr);

        // 删除
        delete arr[3];
        console.log(arr);

        var obj = {
            name: "zhangsan",
            age: 20
        }

        delete obj.name;
        console.log(obj);
    </script>
</body>

</html>

08-instanceof

<!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>
        // 检测数据类型的方法
        // typeof  检测基本数据类型
        var obj = {}
        console.log(typeof obj);

        var arr = [];
        console.log(typeof arr);

        var date = new Date();
        console.log(typeof date);

        // instanceof  适用于引用数据类型
        // 需要程序员明确的确定是否属于某种数据类型

        console.log(arr instanceof Array);
        console.log(date instanceof Date);
        console.log(arr instanceof Date);
        console.log(arr instanceof Object);
    </script>
</body>

</html>

09-总结

<!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>
        // 1.Number 对象 toFixed()  toString()
        // 2.Date 对象 获取当前时间,倒计时
        // 3.Math 对象
        // 4.全局对象
        // 5.instanceof 检测引用数据类型
    </script>
</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值