JavaScript Classes Practice Challenge #3 Message Encryptor

Hi everyone,

I seem to be getting the right answers but with an undefined at the end for the decrypt(). Can anyone see where it is coming from?
I know UTF seems to be how many people did this but I would love to know if its possible with the way I have chosen too.

// Write class below class ShiftCipher { constructor(n) { this.n = n; } // ENCRYPT FUNCTION encrypt(string) { // DECLARE NEW VARIABLES let alphabetUp = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; let code = ''; // CREATE LOWERCASE STRING let uppers = string.toUpperCase(); //TESTER console.log(uppers + "STEP 1") // ITERATE THROUGH STRING for (let i = 0; i < uppers.length; i++) { // FILTER OUT LETTERS ONLY let letter = uppers[i]; //TESTER console.log(uppers[i] + "STEP 2") if (alphabetUp.indexOf(letter) >= 0) { // FIND NEW INDEX let newIndex = alphabetUp.indexOf(letter) + this.n; //TESTER console.log(newIndex + "STEP 3"); // VALID NEW INDEX if (newIndex <= 25) { // ADD NEW LETTER ONTO STRING code += alphabetUp[newIndex]; //TESTER console.log(code + "STEP 4"); // NEGATIVE NEW INDEX CONVERSION } else { let posIndex = newIndex - 26; // ADD NEW LETTER ONTO STRING code += alphabetUp[posIndex]; //TESTER console.log(code + "STEP 4"); } //IF IT IS NOT A LETTER } else { code += letter; //TESTER console.log(code + "STEP 4") } } return code; } // DECRYPT FUNCTION decrypt(string) { // DECLARE NEW VARIABLES let alphabetLow = 'abcdefghijklmnopqrstuvwxyz'; let code = ''; // CREATE LOWERCASE STRING let lowers = string.toLowerCase(); //TESTER console.log(lowers + "STEP 1"); // ITERATE THROUGH STRING for (let i = 0; i <= lowers.length; i++) { // CHECK IF LETTER AND NOT SYMBOL if (alphabetLow.indexOf(lowers[i]) >= 0) { //TESTER console.log(lowers[i]); // FIND NEW POSITION IN ALPHABET let newIndex = alphabetLow.indexOf(lowers[i]) - this.n; //TESTER console.log(newIndex); // CHECK POSITION IS VALID AND CONVERT TO POSITIVE INTEGER if (newIndex < 0) { newIndex += 26; }; // ADD NEW LETTER TO STRING code += alphabetLow[newIndex]; ///TESTER console.log(code); // ADD SYMBOL OR SPACE TO STRING } else { code += string[i]; //TESTER console.log(code); } } return code; } }; const cipher = new ShiftCipher(2); console.log(cipher.encrypt('I love to code!')); // returns 'K NQXG VQ EQFG!' console.log(cipher.decrypt('K <3 OA RWRRA')); // returns 'i <3 my puppy'

TIA!

On line 65 of the CodeByte, you have <= instead of <

1 Like

YES OF COURSE!! THANK YOU SO MUCH!!! :heart_eyes: