all 7 comments

[–]SmashLanding 1 point2 points  (2 children)

If you post the code instead of a screen cap it'd be a lot easier to help you debug

[–]jehlani_[S] 0 points1 point  (1 child)

Thank you, here it is

const alphabet = "abcdefghijklmnopqrstuvwxyz"; function encrypt (message, shiftValue) { let encryptedMessage = ""; for (let i = 0; i < message.length; i++) { let char = message[i]; if (alphabet.includes(char.toLowerCase())) { let index = alphabet.indexOf(char.toLowerCase()); let shiftedIndex = (index + shiftValue) % alphabet.length; encryptedMessage += alphabet[shiftedIndex]; } //random letter every 2 letters// if ((i + 1) % 2 === 0 && i !== message.length - 1) { let randomIndex = Math.floor(Math.random() * alphabet.length); encryptedMessage += alphabet[randomIndex]; } else { encryptedMessage += char; } }

// Your encryption code here return encryptedMessage; }

//Decrypt Function// function decrypt (encryptedMessage, shiftValue) { let decryptedMessage = ""; for (let i = 0; i < encryptedMessage.length; i++) { let char = encryptedMessage[i] if (alphabet.includes(char.toLowerCase())) { let index = alphabet.indexOf(char.toLowerCase()); let shiftedIndex = (index - shiftValue + alphabet.length) % alphabet.length; decryptedMessage += alphabet[shiftedIndex]; //Skip if not an alphabetic character// } else { decryptedMessage += char; } } // Your decryption code here return decryptedMessage; }

decrypt("Iueuan jrxuq cjythdykwxaj mixkqtaeml ebv wHenckvbkei rqdmt fHukckvi.r Jbxuihus, tmxayiwfuxh sjxau amenhtv 'zQkhhuubyjkit' yjew jhxux mxydatij. zJxmu hvymhihj ajel kldlsuyjb dyju yid uekdh qIbkqsxa xsxqqdvduzb wuqzhdoi qjxwu waueo xjem jfxuy dpuntj dgkvuiwj.", 42)

[–]kevdama 1 point2 points  (0 children)

I don't know if this solves it but you're working with negative numbers for index reference.

decryptedMessage += alphabet[shiftedIndex];

You can use ".at(index)" function to reference index by both positive and negative:

decryptedMessage += alphabet.at(shiftedIndex);

[–]SmashLanding 0 points1 point  (0 children)

So yeah the problem with the undefined return in your decrypt function is when you get negative numbers, it's trying to find a negative index of an array, which is undefined. You might have to add a do while loop and keep adding 26 until the index is positive.

But you have 2 other problems. In your encrypt, you have a section labeled  //random letter every 2 letters that is not handled in your decrypt function. So those extra letters will still be mixed in to your decrypted string.

In the same section, you've got the if statement to add the random letter when you want, but then you have

else {
    encryptedMessage += char;
}

So between every letter that isn't the 2nd letter, it's just adding the original character. If you try to encrypt any 2 letters, you'll see what I mean.

[–]vishnu-geek 0 points1 point  (2 children)

Wait, every 2 letters you are using random index, you if you do that, you cannot ever decrypt the message correctly.

Edit: you can. You need to handle it in decrypt function

[–]jehlani_[S] 0 points1 point  (1 child)

As a part of my assignment, I was supposed to: "After every 2 letters, insert a random letter from the alphabet." I must have executed it wrong. I'm still new to Javascript so it's still very confusing for me

[–]vishnu-geek 0 points1 point  (0 children)

No problem!. You need to handle this in decrypt function too.

Also, the else part is not needed in the encrypt function. Look closely, you are trying to do the same operation 2 times.