为了账号安全,请及时绑定邮箱和手机立即绑定

为什么第37行只要我写return flase;页面就没有输出,但函数功能正常?

为什么第37行只要我写return flase;页面就没有输出,但函数功能正常?

Tropicloz 2016-08-27 00:10:04
index.html<!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){             var len1 = arr1.length;             var len2 = arr2.length;             if(len1 == len2 && arr1 instanceof Array && arr2 instanceof Array){                 var types1 = [];                 var types2 = [];                 for(var i = 0; i < len1; i++){                     var a1 = confirmType(arr1[i]);                     if(types1.join().indexOf(a1) == -1){                         types1.push(a1);                     }                     var a2 = confirmType(arr2[i]);                     if(types2.join().indexOf(a2) == -1){                         types2.push(a2);                     }                 }                 var sTypes1 = types1.sort().join();                 var sTypes2 = types2.sort().join();                 if(sTypes1 == sTypes2){                     return true;                 }                 else{                     return false;                 }             }             else{                 return false;             }         }                  function confirmType(obj){             var n = typeof(obj);             if(n == "object"){                 if(obj instanceof Array){                     return "array";                 }                 else if(obj instanceof Date){                     return "date";                 }                 else if(obj instanceof Function){                     return "function";                 }                 else if(obj instanceof Window){                     return "window";                 }                 else if(obj === null){                     return "null";                 }             }             else{                 return n;             }         }     </script>     <script src="testData.js"></script> </body> </html>testData.jsvar result=function(){     //以下为多组测试数据             var cases=[{                     arr1:[1,true,null],                     arr2:[null,false,100],                     expect:true                 },{                     arr1:["444",100],                     arr2:[100,22],                     expect:false                 },{                     arr1:[null,999],                     arr2:[{},444],                     expect:false                 },{                     arr1:[window,1,true,new Date(),"hahaha",(function(){}),undefined],                     arr2:[undefined,(function(){}),"okokok",new Date(),false,2,window],                     expect:true                 },{                     arr1:[new Date()],                     arr2:[{}],                     expect:false                 },{                     arr1:[window],                     arr2:[{}],                     expect:false                 },{                     arr1:[undefined,1],                     arr2:[null,2],                     expect:false                 },{                     arr1:[new Object,new Object,new Object],                     arr2:[{},{},null],                     expect:false                 },{                     arr1:null,                     arr2:null,                     expect:false                 },{                     arr1:[],                     arr2:undefined,                     expect:false                 },{                     arr1:"abc",                     arr2:"cba",                     expect:false                 }];                                   //使用for循环, 通过arraysSimilar函数验证以上数据是否相似,如相似显示“通过”,否则"不通过",所以大家要完成arraysSimilar函数,具体要求,详见任务要求。                 for(var i=0;i<cases.length;i++){                 if(arraysSimilar(cases[i].arr1,cases[i].arr2)!==cases[i].expect) {                     document.write("不通过!case"+(i+1)+"不正确!arr1="+JSON.stringify(cases[i].arr1)+", arr2="+JSON.stringify(cases[i].arr2)+" 的判断结果不是"+cases[i].expect);                     return false;                 }                             }             return true;                      }();     document.write("判定结果:"+(result?"通过":"不通过"));index.html是我的提交,请大家帮忙看看
查看完整描述

1 回答

已采纳
?
elpsycongroo

TA贡献5条经验 获得超3个赞

有组数据为null,导致无法获取arr1、arr2长度,报错。针对arr1、arr2值为null的情况进行判断处理。和37行没有关系

查看完整回答
2 反对 回复 2016-08-27
  • 1 回答
  • 1 关注
  • 1132 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信