samedi 25 février 2017

Using OR operator in an if statement [duplicate]

This question already has an answer here:

In the initial string, passed as an argument to the rot 13 function, the values of the letters are shifted by 13 places. Thus 'A' ↔ 'N', 'B' ↔ 'O' and so on.

I am trying to decode it by iterating through the string and using if else conditional statements. However, it seems that the OR(||) operator is not working properly.

" " has the value 32, so it should stay unchanged, but the result for this charater returns 19,(so 13 was still subtracted from 32).

Where is my mistake?

function rot13(str) { 
  var x="";
  var y = String.fromCharCode(x);
  for(i =0; i < str.length; i++) {
     if(str.charCodeAt(i) > 65 || str.charCodeAt(i) < 78){
        x+= str.charCodeAt(i) - 13 + ",";
      } else if(str.charCodeAt(i) > 78 || str.charCodeAt(i) < 91) {
       x+= str.charCodeAt(i) + 13 + ",";

     }
     else {x+= str.charCodeAt(i) + ",";}


  }

return x; 
//returns '70,56,69,69,19,67,53,68,69,19,67,65,77,54,'
}


rot13("SERR PBQR PNZC");

Aucun commentaire:

Enregistrer un commentaire