简体   繁体   中英

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"). 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. Appreciate the help

You've got two code bodies after your 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 . That's why else is "unexpected".

You are attempting to invoke a statement after you have already completed the if statement. Your if results in the continue; and then does something else before you call the else . Try to refactor the continue; . It doesn't have anything to do with the for loop:)

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

but really just wondering what is the issue with writing an if else statement inside a loop and where im going wrong

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


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

 //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 ; and I've added a group around [nz] so that match[1] aka. down is filled when the character is a nz, but not if the character is am.

Therefore I know, if down is filled, I have to do char.charCodeAt(0) - 13 otherwise char.charCodeAt(0) + 13

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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