错误时候显示错误,那什么都不显示是啥意思???

来源:1-6 编程练习

慕丝9577611

2019-03-15 15:01

function arraysSimilar(arr1, arr2){            
    var count = 0;            
    if (arr1.length === arr2.length) {                
        arr1.forEach(function (x) {                    
            switch(typeof x) {                        
                case "number":                        
                case "string":                        
                case "boolean":                        
                case "undefined":                        
                case "function":                            
                    var result = arr2.some(item => (typeof item) === (typeof x));                            
                    if (result === true) {                                
                        count++;                            
                    }                            
                    break;                        
                case "object":                            
                    var result = objRec(x);                            
                    result = arr2.some(item => item === result);                            
                    if (result === true) {                                
                        count++;                            
                    }                            
                    break;                    
                  }                
            });                
            if (count === arr1.length) {               
              return true;                
            }                
            else {                  
              return false;                
            }            
    }            
    return false;        
}        
function objRec(x) {            
    if (x === null) {                
        return null;            
    }            
    else if (x instanceof Date) {                
        return Date;            
    }            
    else if (x != null && x === x.window) {               
        return window;            
    }        
}


写回答 关注

1回答

  • 慕丝9577611
    2019-03-15 18:58:17

    行吧...我自己找到问题了,貌似我代码哪里出了问题,testData.js的json读取出错。

    修改了下,运行成功,显示通过。

    function arraysSimilar(arr1, arr2){            
        var count = 0;            
        if (!(arr1 instanceof Array) || !(arr2 instanceof Array)) {                
            return false;            
        }            
        if (arr1.length === arr2.length) {                
            arr1.forEach(function (x) {                    
            switch(typeof x) {                        
            case "number":                        
            case "string":                        
            case "boolean":                        
            case "undefined":                        
            case "function":                            
                var result = arr2.some(item => (typeof item) === (typeof x));                            
                if (result === true) {                                
                count++;                            }                            
                break;                        
            case "object":                            
                if (x instanceof Date) {                                
                    if(arr2.some(item => item instanceof Date)) {                                    
                        count++;                                
                    }                            
                }                            
                else {                                
                    var result = objRec(x);                                
                    result = arr2.some(item => item === result);                                         
                    if (result === true) {                                    
                        count++;                                
                    }                            
                 }                            
                 break;                    
            }                
        });                
        if (count === arr1.length) {                    
            return true;                
        }                
        else {                    
            return false;                
        }            
    }            
    return false;        
    }        
    function objRec(x) {            
        if (x === null) {                
            return null;            
        }            
        else if (x instanceof Date) {                
            return Date();            
        }            
        else if (x != null && x === x.window) {                
            return window;            
        }            
        else if (x instanceof Array) {                
            return Array;            
        }            
        else {                
            return Object;            
        }        
    }


JavaScript深入浅出

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

281112 学习 · 1020 问题

查看课程

相似问题