0

const alphabet = 'abcdefghijklmnopqrstuvwxyz';
let code = 2;

function decodeMessage(message, secret) {
    let decoded = ''


    for (let i = 0; i < message.length; i++) {
        let charNum = message.charCodeAt(i);



          decoded += String.fromCharCode(((message.charCodeAt(i) + secret - 97) % 26) + 97);
      

        }
        return decoded
   
}


const message = 'hello, how are you doing, my general?';
const secret_msg = decodeMessage(message, code);
console.log('Secret =>', secret_msg);

setTimeout(() => {
    code *= -1;
    const message_encoded = decodeMessage(secret_msg, code);
    console.log('Original =>', message_encoded);
}, 3000)

I was expecting it to return: 'hello, how are you doing, my general?', but instead, it converts symbols and space into other symbols and capitalized letters. I do not use and need capitalized letters in the cipher.

Secret => jgnnqHVjqyVctgVaqwVfqkpiHVoaVigpgtcn[

Original => helloThowTareT_ouTdoingTm_TgeneralY

Thank you.

Safe House
  • 15
  • 2

2 Answers2

0

const alphabet = 'abcdefghijklmnopqrstuvwxyz';
let code = 2;

function decodeMessage(message, secret) {
    let decoded = ''


    for (let i = 0; i < message.length; i++) {
        let charNum = message.charCodeAt(i);

console.log(charNum +' = '+((message.charCodeAt(i) + secret - 97) % 26) + 97);

          decoded += String.fromCharCode(((message.charCodeAt(i) + secret - 97) % 26) + 97);
      

        }
        return decoded
   
}


const message = ' ';
const secret_msg = decodeMessage(message, code);
console.log('Secret =>', secret_msg);

setTimeout(() => {
    code *= -1;
    const message_encoded = decodeMessage(secret_msg, code);
    console.log('Original =>', message_encoded);
}, 3000)

because you have the wrong algorithm. you assumed that the characters start with 97 - but that's not true because the space has the code 32. see the snipet - the coded character for the space

bat : you can replace the spaces with some character from the range - this is not the best solution to the problem

const alphabet = 'abcdefghijklmnopqrstuvwxyz';
let code = 2;

function decodeMessage(message, secret) {
    let decoded = ''


    for (let i = 0; i < message.length; i++) {
        let charNum = message.charCodeAt(i);

console.log(charNum +' = '+((message.charCodeAt(i) + secret - 126) % 26) + 126);

          decoded += String.fromCharCode(((message.charCodeAt(i) + secret - 126) % 26) + 126);
      

        }
        return decoded
   
}


const message = 'fg 345 ert';
const secret_msg = decodeMessage(message.replace(/\s+/g, '~'), code);
console.log('Secret =>', secret_msg);

setTimeout(() => {
    code *= -1;
    const message_encoded = decodeMessage(secret_msg, code);
    console.log('Original =>', message_encoded.replace(/\~+/g, ' '));
}, 30)
WiatroBosy
  • 1,076
  • 1
  • 6
  • 17
  • Caesar cipher uses a simple shift of letters within the alphabet, hence the 97 value (so that A is 0, B is 1, ...). See https://en.wikipedia.org/wiki/Caesar_cipher – Fabien Quatravaux Jan 02 '23 at 21:28
  • A is 0, B is 1, ... ok but what is the value of a space or #, !, $ – WiatroBosy Jan 03 '23 at 06:35
  • calculations for spaces ((32 + 2 -97)%26)+97 = 86 = v ((32 - 2 -97)%26)+97 = 82 = R as you can see this algorithm cannot correctly reproduce spaces and other characters below 97 asci code – WiatroBosy Jan 03 '23 at 06:42
  • read this https://stackoverflow.com/questions/44232645/caesar-cipher-in-javascript – WiatroBosy Jan 03 '23 at 07:06
0

You have two issues in your code : the punctuation should not be ciphered, and when secret is negative, the ciphered character underflows the alphabet list.

To avoid ciphering the punctuation, you could use a regular expression. The regex below use \w to match word letters and set punctuation apart. Then each word is ciphered with your Caesar algorithm and punctuation is set up again at its place.

To fix the second issue (that shows _ instead of y in your example), you can add 26 before applying the modulo, to guarantee to work with a positive number before adding 97.

Here is the fixed decodeMessage function :

function decodeMessage(message, secret) {
  let decoded = ''

  // step through words using a regular expression
  let expression = new RegExp(/([^\w]*)(\w+)([^\w]*)/g);
  let result = expression.exec(message);

  // global regex exec method will return null as soon as no more match is found
  while(result){
    decoded += result[1];
    for (let i = 0; i < result[2].length; i++) {
      let charNum = result[2].charCodeAt(i);
      decoded += String.fromCharCode((((result[2].charCodeAt(i) + secret - 97 + 26) % 26) + 97));
    }
    decoded += result[3];

    // match the next word
    result = expression.exec(message);
  }
  return decoded 
}
Fabien Quatravaux
  • 3,737
  • 2
  • 27
  • 33
  • thank you! but what if I add special symbols, but don't want them to be converted? for example: `hello,*-*+how are you doing, my general?` currently, it returns me: 'hellohow are you doing, my general? – Safe House Jan 03 '23 at 06:56
  • Then you should modify the regexp to also match those symbols. With this regex it works : `/([^\w]*)(\w+)([^\w]*)/g` – Fabien Quatravaux Jan 03 '23 at 16:55
  • amazing! thank you! I also found a solution using array map method – Safe House Jan 04 '23 at 07:17