Bob*_*llh 0 javascript for-loop if-statement
function rot13(str) {
var yahoo = [];
for (var i = 0; i < str.length; i++) {
if (str.charCodeAt(i) > 64 && str.charCodeAt[i] < 91){continue;}{
var cnet = str.charCodeAt(i);
yahoo.push(cnet);
} else {
var j = str.charCodeAt(i);
yahoo.push(j);
}
}
var ugh = yahoo.toString();
return ugh;
}
rot13("SERR PBQR PNZC");
Run Code Online (Sandbox Code Playgroud)
试图在for循环中使用if else语句并在else语句中遇到一些问题(获取"语法错误:意外的令牌其他").现在的主要目标是尝试操纵字符串字母字符,同时传递其他字符(即空格,感叹号等).当然有一种更简单的方法可以做到这一点,但实际上只是想知道在循环中写一个if else语句和我出错的问题是什么.感谢帮助
你的后面有两个代码体if
:
if (str.charCodeAt(i) > 64 && str.charCodeAt[i] < 91)
{continue;} // actual body of the if
{ // just a random block of code
var cnet = str.charCodeAt(i);
yahoo.push(cnet);
}
Run Code Online (Sandbox Code Playgroud)
第二个不是它的一部分if
,因为你只得到一个代码块if
.这else
就是"出乎意料" 的原因.
归档时间: |
|
查看次数: |
81 次 |
最近记录: |