Wyatt G. Railey
Wyatt G. Railey

Reputation: 19

code is not bypassing first "if" statement

Could someone provide insight on why my code is not bypassing the first "if" statement? I am writing code that is supposed to create a cipher. If the letter's unicode is not greater than 97 or less than 122, it is is supposed to simply push the unicode as it is originally stated.

// Write class below
class ShiftCipher{
  constructor(shift){
    this._shift = shift;
  }
  encrypt(str){
    let newStr = str.toLowerCase();
    let strArr = [];
    let newStrArry = [];
    let extraNum = 0;
    let bigArr = [strArr, newStrArry]
    for(let i = 0; i < newStr.length; i++){
      strArr.push(newStr.charCodeAt(i));
      if(newStr.charCodeAt(i) > 97 || newStr.charCodeAt(i) < 122) {
        if(newStr.charCodeAt(i)+this._shift > 122){
          extraNum = (newStr.charCodeAt(i)+this._shift) - 122;
          extraNum += newStrArry.push(96+extraNum);
          console.log('a');
          } else {
          newStrArry.push(newStr.charCodeAt(i)+this._shift);
          console.log('b');
          console.log(newStr[i]);
          }
      } else {
        newStrArry.push(newStr.charCodeAt(i));
        console.log('c');
      }
    }
    return bigArr;
  }
}
const mySymbol = new ShiftCipher(4);
console.log(mySymbol.encrypt('<3'));

Upvotes: 0

Views: 47

Answers (1)

General Poxter
General Poxter

Reputation: 554

The very condition newStr.charCodeAt(i) > 97 || newStr.charCodeAt(i) < 122 is satisfied by every possible number (i.e. any number that is not greater than 97 has to be less than 122). Thus, no input would make it to your else clause.

I assume you wanted it such that only unicodes between 97 and 122 are accepted by the if condition. If that's the case, then you need to change the condition to newStr.charCodeAt(i) > 97 && newStr.charCodeAt(i) < 122.

Upvotes: 2

Related Questions