简体   繁体   English

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

[英]JavaScript - If statement inside a loop issues

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

Attempting to use an if else statement inside a for loop and having some issues with the else statement (Getting "Syntax error: unexpected token else"). 尝试在for循环内使用if else语句,并使else语句出现一些问题(获取“语法错误:意外的标记else”)。 Main goal right now is to try to manipulate the strings alphabet characters while passing the other characters through (ie. spaces, exclamation points etc.). 现在的主要目标是尝试在传递其他字符(例如空格,感叹号等)的同时操纵字符串字母字符。 Sure there is an easier way of doing that but really just wondering what is the issue with writing an if else statement inside a loop and where im going wrong. 当然,有一种更简单的方法可以做到这一点,但实际上只是想知道在循环中编写if else语句是什么问题,以及我哪里出错了。 Appreciate the help 感谢帮助

You've got two code bodies after your if : 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);
  }

The second one is not part of the if at all, because you only get one code block for the if . 第二个根本不是if一部分,因为您只会为if获得一个代码块。 That's why else is "unexpected". 这就是为什么else “意外”的原因。

You are attempting to invoke a statement after you have already completed the if statement. 您已经在完成if语句之后尝试调用一条语句。 Your if results in the continue; 您的if导致continue; and then does something else before you call the else . 然后在调用else之前做其他else Try to refactor the continue; 尝试重构continue; . It doesn't have anything to do with the for loop:) 它与for循环无关:)

Attempting to use an if else statement inside a for loop and having some issues with the else statement (Getting "Syntax error: unexpected token else"). 尝试在for循环内使用if else语句,并使else语句出现一些问题(获取“语法错误:意外的标记else”)。

but really just wondering what is the issue with writing an if else statement inside a loop and where im going wrong 但真的只是想知道在循环中编写if else语句是什么问题,以及我在哪里出错

that you don't write an if..else statement, but an if statement and a code block where you try to add your else statement; 您不是在编写if..else语句,而是在编写试图添加else语句的if语句和代码块; and this else-statement doesn't make sense there. 而且其他陈述在那里没有意义。

your code reads like this: 您的代码如下所示:

//this is your condition
if (str.charCodeAt(i) > 64 && str.charCodeAt[i] < 91){
    continue;
}

//and this is an anonymous code block; anonymous, because you could name it
{
    var cnet = str.charCodeAt(i);
    yahoo.push(cnet);

//and such code-blocks have no `else`,
//that's why you get the error, 
//this else doesn't belong to the condition above
} else {
    var j = str.charCodeAt(i);
    yahoo.push(j);
}

your problem is the {continue;} part that changes the whole menaing of your blocks to what I've described 您的问题是{continue;}部分,它将您的方块的整体改变为我所描述的


Sure there is an easier way of doing that 当然,有一种更简单的方法

yes, you could use String#replace , and replace the letters am with nz and vice versa 是的,您可以使用String#replace ,并将字母am替换为nz ,反之亦然

 //a little helper const replace = (needle, replacement) => value => String(value).replace(needle, replacement); //the definition of `rot13` as a String replacement const rot13 = replace( /[am]|([nz])/gi, (char,down) => String.fromCharCode(char.charCodeAt(0) + (down? -13: 13)) ); let s = "SERR PBQR PNZC"; console.log("input: %s\\noutput: %s", s, rot13(s)); 

explanation: match[0] always contains the whole matched string, here this is the char ; 说明: match[0]始终包含整个匹配的字符串,这里是char and I've added a group around [nz] so that match[1] aka. 并且我在[nz]周围添加了一个群组,从而使match[1]又名。 down is filled when the character is a nz, but not if the character is am. 当字符是nz时,将填充down ,但如果字符是am,则不填充。

Therefore I know, if down is filled, I have to do char.charCodeAt(0) - 13 otherwise char.charCodeAt(0) + 13 因此,我知道,如果down填充,我必须执行char.charCodeAt(0) - 13否则char.charCodeAt(0) + 13

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM