JavaScript - 循环内的 If 语句出现问题

标签 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");

尝试在 for 循环内使用 if else 语句,但 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);
  }

第二个根本不是 if 的一部分,因为您只能获得 if一个代码块。这就是为什么 else 是“意外的”。

关于JavaScript - 循环内的 If 语句出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49465030/

相关文章:

javascript - 使用 Angular 2 跳过浏览器历史记录中的跳转链接

javascript - react-select 下拉菜单在模态内打开

C 中的复合关系运算符

if-statement - 对话流 : Respond based on values and other conditional responses

javascript正则表达式验证带空格的字母数字文本并拒绝特殊字符

javascript - 将 async/await 与 promise 和 appendChild 结合使用时,应用程序停止工作

c - while (getchar () != '\n' );

for-loop - swift 3 : how to write this for(;;) loop

C++ 嵌套 For 循环

selenium webdriver 中的 Java else if 语句 - 阴性测试