• 实现Math对象中max和min方法


    
    
        function mymin(num1) {
            if (arguments.length == 0) {
                // 没有参数
                return Infinity;
            } else if (arguments.length == 1) {
                // 一个参数
                return Number(num1);
            } else {
                // 至少两个参数
                // console.log(arguments);
                // 设最小值为第一个值
                var min = arguments[0];
                // 循环比较
                for (var i = 1; i < arguments.length; i++) {
                    if (arguments[i] < min) {
                        // 判断当前的值是否小于最小值,如果小于最小值,则将该值赋值给最小值
                        min = arguments[i];
                    }
                    // console.log(arguments[i]);
                }
                // 返回最小值
                return min;
            }
        }
        console.log(mymin());// Infinity
        console.log(mymin(1));// 1
        console.log(mymin('i love you'));// NaN 不需要去考虑
        console.log(mymin(true));// 1 不需要去考虑
        console.log(mymin(1, 3, 13, 12, 2, 5));// 13 
    
    
    
    function mymax(num1) {
            if (arguments.length == 0) {
                // 没有参数
                return -Infinity;
            } else if (arguments.length == 1) {
                // 一个参数
                return Number(num1);
            } else {
                // 至少两个参数
                // console.log(arguments);
                // 设最大值为第一个值
                var max = arguments[0];
                // 循环比较
                for (var i = 1; i < arguments.length; i++) {
                    if (arguments[i] > max) {
                        // 判断当前的值是否大于最大值,如果大于最大值,则将该值赋值给最大值
                        max = arguments[i];
                    }
                    // console.log(arguments[i]);
                }
                // 返回最大值
                return max;
            }
        }
        console.log(mymax());// -Infinity
        console.log(mymax(1));// 1
        console.log(mymax('i love you'));// NaN 不需要去考虑
        console.log(mymax(true));// 1 不需要去考虑
        console.log(mymax(1, 3, 13, 12, 2, 5));// 13 
  • 相关阅读:
    Centos7.0 安装Oralce 11g数据库
    python学习:基础数据类型
    Centos7.0 安装MySQL数据库
    Centos7.0 安装MariaDB数据库
    微服务的详情
    Class -- 10 -- Method类常用方法解析
    遍历list的三种方式
    使用@Autowired注解警告Field injection is not recommended
    java中的两种排序工具Arrays和Collections的使用
    java的reflection和introspector
  • 原文地址:https://www.cnblogs.com/yess/p/12632558.html
Copyright © 2020-2023  润新知