求问,哪里出问题了?

来源:1-6 编程练习

斑猪拖拖

2020-02-19 13:59

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb18030">
    <title>Untitled Document</title>
    
</head>
<body>
    <script type="text/javascript">   
        /*
         * param1 Array
         * param2 Array
         * return true or false
         */
        function arraysSimilar(arr1, arr2){
            if(arr1 instanceof Array&& arr2 instanceof  Array){
                if(arr1.length==arr2.length){
                    a1=arr1.sort();
                    a2=arr2.sort();
                    for(i=0,i<arr1.length,i++){
                        if(Object.prototype.toString.apply(a1[i]) == Object.prototype.toString.apply(a2[i])){
                            return true;
                        }
                    }                
                }else{
                    return false;
                }
            }else{
                return false;
            }
        
        }
    </script>
    <script src="testData.js"></script>
</body>
</html>



写回答 关注

2回答

  • qq_浪民_cpxyM5
    2020-03-04 15:41:51

    <!DOCTYPE HTML>

    <html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=gb18030">

        <title>Untitled Document</title>

        

    </head>

    <body>

        <script type="text/javascript">   

            /*

             * param1 Array 

             * param2 Array

             * return true or false

             */

            var allList = []

            function arraysSimilar(arr1, arr2){

                let typeList1 = getTypeList(arr1)

                let typeList2 = getTypeList(arr2)

                return (typeList1.length  === allList.length) && (typeList2.length  === allList.length)

            }

            function getTypeList(arr) {

                let typeList = []

                arr.forEach(item => {

                    let type = Object.prototype.toString.call(item)

                    if (typeList.indexOf(type) < 0) {

                        typeList.push(type)

                    }

                    if (allList.indexOf(type) < 0) {

                        allList.push(type)

                    }

                })

                return typeList

            }

        </script>

        <script src="testData.js"></script>

    </body>

    </html>


  • qq_浪民_cpxyM5
    2020-03-04 15:00:14

      if(arr1 instanceof Array&& arr2 instanceof  Array) {...}else{return false}

    这句话的意思是如果arr1和arr2都是Array的实例的话就执行...,否则返回false。这样写的话如果两个不是Array就会返回false.

JavaScript深入浅出

由浅入深学习JS语言特性,且解析JS常见误区,从入门到掌握

281111 学习 · 1020 问题

查看课程

相似问题