Weird assertion problem in "JavaScript Practice: Classes"

Dear community,

I have stumbled into a weird issue when clicking “check answer” on “JavaScript Practice: Classes”, (JavaScript Syntax, Part III) (the third challenge) - Link: https://www.codecademy.com/paths/front-end-engineer-career-path/tracks/fecp-javascript-syntax-part-iii/modules/fecp-practice-javascript-syntax-classes/articles/fecp-javascript-practice-classes

When I click “Check answer”, I get the message

“Tested method encrypt('Z') with a shift of 1 , expected 'A' . In the encrypt() method, did you handle wrapping around to the beggining of the alphabet?”

However, when I run my code with "encrypt(‘Z’), I get ‘A’ returned. Therefore, the assertion code should not react on this.

My code to reproduce:

// Write class below
class ShiftCipher {
  constructor(shift) {
    this.shift = shift
  }

  charIsLetter(char) {
    return (/[a-zA-Z]/).test(char);
  }

  encrypt(string) {
    let encryptedString = ""
    for (let i = 0; i <= string.length; i++) {
      if (this.charIsLetter(string[i])) {
        let UTFencryptedIndex = string.charCodeAt(i) + this.shift;
      
        if ((UTFencryptedIndex > 90 && UTFencryptedIndex < 97) || UTFencryptedIndex > 122) {
          UTFencryptedIndex -= 26
        }
        encryptedString += String.fromCharCode(UTFencryptedIndex)
      } else {
        encryptedString += string[i]
      }
    }
    return encryptedString.toUpperCase();;
  }

  decrypt(string) {
    let decryptedString = ""

    for (let i = 0; i <= string.length; i++) {
      if (this.charIsLetter(string[i])) {
        let UTFencryptedIndex = string.charCodeAt(i) - this.shift;

        if (UTFencryptedIndex < 65) {
          UTFencryptedIndex += 26;
        }

        decryptedString += String.fromCharCode(UTFencryptedIndex)
      } else {
        decryptedString += string[i]
      }
    }
    return decryptedString.toLowerCase();
  }
}

const cipher = new ShiftCipher(1);

console.log(cipher.encrypt('Z'));

// console.log(cipher.decrypt('K <3 OA RWRRA'));

Any ideas?

(Also, just noticed there is a typo in the assertion message, “beggining”, if a mod sees this)

Hey!

I get the same issue as you. My code is far less elegant than yours, but (as far as I can tell) it still gets the job done. It reliably decrypts anything I’ve encrypted, and it returns the same results as in the examples. Yet, like yours, it fails to pass the Codecademy test.

I’m posting a Codebyte below as proof.

I wonder if there is any way to tag a moderator to get them to check if we’ve found a flaw in the tester. @codecademy, maybe?

Thanks a bunch in advance to any mod/staff member that responds to this.

(P.S. I realise my indentation leaves a lot to be desired. I can only apologise and offer as an excuse: the CodeChallenge box is rather small.)

// Write class below class ShiftCipher { constructor(number) { this.number = number; } encrypt(string) { string = string.toLowerCase(); let shiftedCharCodes = []; for (let i=0; i <= string.length; i++) { if (string.charCodeAt(i) > 122 || string.charCodeAt(i) < 97) { shiftedCharCodes.push(string.charCodeAt(i)); } else { let toShift = string.charCodeAt(i) + this.number; if (toShift > 122) { toShift = 97 + (toShift - 123); } shiftedCharCodes.push(toShift); } }; return String.fromCharCode(...shiftedCharCodes).toUpperCase(); } decrypt(string) { string = string.toLowerCase(); let shiftedCharCodes = []; for (let i=0; i <= string.length; i++) { if (string.charCodeAt(i) > 122 || string.charCodeAt(i) < 97) { shiftedCharCodes.push(string.charCodeAt(i)); } else { let toShift = string.charCodeAt(i) - this.number; if (toShift < 97) { toShift = 123 - (97 - toShift); } shiftedCharCodes.push(toShift); } }; return String.fromCharCode(...shiftedCharCodes); } } let test = new ShiftCipher(1); console.log(test.encrypt('Z'));