函数generateHashtag(str);
传递一个必须符合以下条件的字符串:
输出示例输入:
"您好,感谢您尝试我的Kata" => "#HelloThereThanksForTryingMyKata"
" Hello World" => "#的HelloWorld"
到目前为止,这是我的代码:
function generateHashtag (str) {
if (!str) {
return false;
} else {
var los = str.toLowerCase();
var res = los.replace(/\b./g, function(m){ return m.toUpperCase(); });
}
if ( res.charAt( 0 ) != '#' ) {
res = "# " + res;
} else {
res = "" + res;
}
if (res.length > 140) {
return false;
} else {
return res;
}
}
这是codewar.com网站的编码挑战。我在那里收到以下消息Test didn't pass: Unknown error
。它适用于js fiddle link to js fiddle
答案 0 :(得分:1)
这有用吗?
function generateHashtag (str) {
if(!str || str.length == 0 || (str.replace(/([^a-zA-Z\s])/g, "").length + 1) > 140)
return false;
var finalString = "";
str = str.replace(/([^a-zA-Z\s])/g, "").trim().toLowerCase().split(" ");
for(i in str)
finalString += str[i].charAt(0).toUpperCase() + str[i].slice(1);
return "#" + finalString;
}