javascript hash表写法 以及 单元测试的例子

以下的例子完成功能,就是输入省市的拼音,返回中文的表达式。

涉及到hash类,以及单元测试

varsheng={

"shanxi":"山西",

"Shaanxi":"陕西",

"Chekiang":"浙江",

"Zhejiang":"浙江",

"Nanking":"南京",

"Nanjing":"南京",

"Hohhot":"呼和浩特",

"Tientsin":"天津",

"Tianjin":"天津",

"Harbin":"哈尔滨",

"Szechwan":"四川",

"Sichuan":"四川",

"Amoy":"厦门",

"Foochow":"福州",

"transformUpperCase":function(myinput){

myinput=myinput.replace(/\w/g,function(s){returns.toLowerCase();});

myinput=myinput.replace(/\s/g,"");

return(myinput);

},

"getCN":function(myinput){

return(sheng[myinput]);//返回类属性值,也可以看做哈希表值,是用【】来取得

}

}

functionassert(bCondition,sErrorMsg){

if(!bCondition){

console.log(sErrorMsg);

//thrownewError(sErrorMsg);

}else{

console.log("passed");

}

}

/*单元测试

assert(sheng.transformUpperCase("ShanXi")=="shanxi","testfailed");

assert(sheng.transformUpperCase("Shanxi")=="shanxi","testfailed");

assert(sheng.transformUpperCase("ShanXi")=="shanxi","testfailed");

*/

console.log(sheng.getCN(sheng.transformUpperCase("Shanxi")));

console.log(sheng.getCN("shanxi"));

相关推荐