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

I know I’m resurrecting the dead here, but I couldn’t find any other posts on the topic and I believe our issue is the same. I would like to know if there is genuinely something wrong with my code, or if this is just an error with the checker functionality.

class ShiftCipher { constructor(key) { this.key = key; }; encrypt(message) { message = message.toString().toUpperCase(); const encryptedMessage = []; for (let i = 0; i < message.length; i++) { if (message.charCodeAt(i) < 65 || message.charCodeAt(i) > 90) { encryptedMessage.push(message.charCodeAt(i)); } else if (message.charCodeAt(i) + this.key > 90) { encryptedMessage.push(message.charCodeAt(i) + this.key - 26); } else { encryptedMessage.push(message.charCodeAt(i) + this.key); }; }; return console.log(String.fromCharCode.apply(null,encryptedMessage)); }; decrypt(encryption) { encryption = encryption.toString() const decryptedMessage = []; for (let i = 0; i < encryption.length; i++) { if (encryption.charCodeAt(i) < 65 || encryption.charCodeAt(i) > 90) { decryptedMessage.push(encryption.charCodeAt(i)); } else if (encryption.charCodeAt(i) - this.key < 65) { decryptedMessage.push(encryption.charCodeAt(i) - this.key + 26); } else { decryptedMessage.push(encryption.charCodeAt(i) - this.key); }; }; return console.log(String.fromCharCode.apply(null,decryptedMessage).toLowerCase()); }; }; const cipher = new ShiftCipher(2); cipher.encrypt('I love to code!'); // returns 'K NQXG VQ EQFG!' cipher.decrypt('K <3 OA RWRRA'); // returns 'i <3 my puppy' const zCipher = new ShiftCipher(1); zCipher.encrypt('Z'); //returns A

The error I receive upon checking is:

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?

I am not a subscriber, so I can’t view the specifications in the instructions.

But, you are returning undefined not the strings. The return value of a console.log statement is undefined. You have return console.log(...) in your code. I suspect the instructions want you to return strings. Printing to the console can be done outside the class.

1 Like

You are a lifesaver! This was driving me crazy. I know I’ve run into the problem before but I guess it never really clicked with me that the reason you must omit it is that the return value of console.log is undefined.

Once I removed those from the return statement it validated successfully. Thank you!!!

1 Like