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.js
var 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是我的提交,请大家帮忙看看
elpsycongroo
相关分类