js第四天-函数

例1:选出最大值:

    <script>
        function getmax(x, y) {
            return x > y ? x : y
        }
        let max = getmax(1, 3)
    </script>

例2:返回数组的最大值

    <script>
        function getArrValue(arr = []) {
            let max = arr[0]
            for (let i = 1; i < arr.length; i++) {
                if (arr[i] > max) max = arr[i]

            }
            return max
        }

        let a = getArrValue([1, 5, 6, 8, 3, 2, 4, 7])
        document.write(a)
    </script>

例3:时间转换

    <script>
        let second = prompt('请输入秒数')
        function getTime(t) {
            d = parseInt(t / 60 / 60 / 24)
            h = parseInt(t / 60 / 60 % 24)
            m = parseInt(t / 60 % 60)
            s = parseInt(t % 60)
            if (d < 10) d = '0' + d
            if (h < 10) h = '0' + h
            if (m < 10) m = '0' + m
            if (s < 10) s = '0' + s

            document.write(`
        ${second}秒转换之后为
            ${d}天${h}小时${m}分钟${s}秒
            `)

        }

        getTime(second)
    </script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值