Classes I had the correct answer but not accepted for some reason

On one of their JavaScript Classes exercise/project the project about making “class ShiftCipher”.
Where I am suppose to make a cipher so if the user inputs ‘helloworld’ it gets shifted encrypted by whatever the shift number say its shift = 3 so output should be ‘khoozruog’. then when i decrypt ‘khoozruog’ would return back ‘helloworld’. Not only this I as well address if the user input is ‘z’ or ‘x’ (later letters) and the shift number is 5 then I made the program restart with the letters back from ‘a’. Then the same when it comes for decrypting and the user input is ‘a’ or ‘b’ (early letters) and shift number is 5 then I made the program count backwards from ‘z’

The thing is I done my exercise until I got the right answer basically on the console completely different from the logic and approach taken by codeacademy but it got rejected by codecademy and I checked their answer they have a different approach then me. here is my code which does work btw
My program below

// Write class below
const alphabet = "a b c d e f g h i j k l m n o p q r s t u v w x y z";
const ari = alphabet.split(' ');
//console.log(ari[25])//z
class ShiftCipher {
  constructor(shift) {
    this.shift = shift;
  }
  encrypt(str) {
    let encryptedWord = "";
  const str1 = str.toLowerCase();
    const str2Array = str1.split('').join(' ').split(' ');
  const testArray = [];
      str2Array.map(arr => {
      arr = ari.indexOf(arr);
        if (arr + this.shift >= 26) {
          return testArray.push(ari[arr + this.shift - 26]);
        } else {
          return testArray.push(ari[arr + this.shift]);
        }
    });
  encryptedWord += testArray.join("");
  return encryptedWord;
}
  decrypt(str) {
    let decryptedWord = "";
    const str1 = str.toLowerCase();
    const str2Array = str1.split('').join(' ').split(' ');
  const testArray = [];
      str2Array.map(arr => {
      arr = ari.indexOf(arr);
        if (arr - this.shift < 0) {
          return testArray.push(ari[arr - this.shift + 26]);
        } else {
          return testArray.push(ari[arr - this.shift]);
        }
    });
  decryptedWord += testArray.join("");
  return decryptedWord;
  }
}
const ciph = new ShiftCipher(5);
console.log(ciph.encrypt('zoz'));//'wordtobeencrypted'
console.log(ciph.decrypt('beb'));//wordtobedecrypted

here below is how they did it

// Write class below
class ShiftCipher {
  constructor(shift){
    this.shift = shift;
  }
  encrypt(plainString) {
    let encryptString = '';
    const tempString = plainString.toUpperCase();

    for (let i=0; i < tempString.length; i++) {
      let charNum = tempString.charCodeAt(i);
      
      if (charNum <= 90 && charNum >= 65) {
        charNum += this.shift;
        if (charNum > 90) {
          charNum -= 26;
        }
      }
      encryptString += String.fromCharCode(charNum);
    }
    return encryptString;
  }

  decrypt(encryptString) {
    let decryptString = '';
    const tempString = encryptString.toLowerCase();

    for (let i=0; i < tempString.length; i++) {
      let charNum = tempString.charCodeAt(i);
      
      if (charNum <= 122 && charNum >= 97) {
        charNum -= this.shift;
        if (charNum < 97) {
          charNum += 26;
        }
      }
      decryptString += String.fromCharCode(charNum);
    }
    return decryptString;
  }
}

Yes sometimes that happens, if your logic works, then it’s fine.
Sometimes to pass Codeacademy exercises you need to write letter by letter as the steps suggest. Is not a bug, is just the way the exercise is automatically corrected, if something doesn’t match it gives you an error. :slight_smile:

1 Like