Message Mixer Steps 10 & 11

Hello Community,

I am currently stuck with the very last steps of Message Mixer projects:

** https://www.codecademy.com/paths/create-a-back-end-app-with-javascript/tracks/bapi-javascript-objects-modules-and-browser-compatibility/modules/bapi-node-modules/projects/message-mixer-node**

For the step 10, I typed in caesarCipher(symbolCipher(reverseCipher(str)), -6); to match encodeMessage() . Then for the step 11 (the very last step), I tried running the program in encodeMessage() and decodeMessage(). The decode one doesn’t work, it always encodes anything I type in:

Unbenannt

Here’s how my code looks like, could anyone kindly point out what might have gone wrong?

super-encoder.js

// Import the functions from encryptors.js here.
const encryptors = require('./encryptors.js');
// Encryption Functions
/////////////////////////////////////////////
const {caesarCipher, symbolCipher, reverseCipher} = encryptors;


// User Input / Output Logic
/////////////////////////////////////////////

const encryptionMethod = getEncryptionMethod();
process.stdin.on('data', (userInput) => {
  displayEncryptedMessage(encryptionMethod, userInput);
});

/* Helper function for determining which cipher method
the user chose when they ran the program. */
function getEncryptionMethod() {
  let encryptionMethod;
  
  const encryptionType = process.argv[2];  
  if (encryptionType === 'symbol') {
    encryptionMethod = symbolCipher;
  } else if (encryptionType === 'reverse') {
    encryptionMethod = reverseCipher;
  } else if (encryptionType === 'caesar') {
    let amount = Number(process.argv[3]);
    if (Number.isNaN(amount)) {
      process.stdout.write(`Try again with a valid amount argument. \n`)
      process.exit();  
    }
    encryptionMethod = (str) => caesarCipher(str, amount);
  } 
  else {
    process.stdout.write(`Try again with a valid encryption type. \n`)
    process.exit();
  }

  process.stdout.write('Enter the message you would like to encrypt...\n> ');
  return encryptionMethod;
}

/* Helper function for displaying the encrypted message to the user. */
function displayEncryptedMessage(encryptionMethod, userInput) {
  let str = userInput.toString().trim();    
  let output = encryptionMethod(str);
  process.stdout.write(`\nHere is your encrypted message:\n> ${output}\n`)
  process.exit();
}

encryptors.js

// Declare and export the functions here.
const caesarCipher = (str, amount = 0) => {
  if (amount < 0) {
    return caesarCipher(str, amount + 26);
  }
  let output = '';
  for (let i = 0; i < str.length; i++) {
    let char = str[i];
    if (char.match(/[a-z]/i)) {
      let code = str.charCodeAt(i);
      if (code >= 65 && code <= 90) {
        char = String.fromCharCode(((code - 65 + amount) % 26) + 65);
      } else if (code >= 97 && code <= 122) {
        char = String.fromCharCode(((code - 97 + amount) % 26) + 97);
      }
    }
    output += char;
  }
  return output;
};

const symbolCipher = (str) => {
  const symbols = {
    'i': '!',
    '!': 'i',
    'l': '1',
    '1': 'l',
    's': '$',
    '$': 's',
    'o': '0',
    '0': 'o',
    'a': '@',
    '@': 'a',
    'e': '3',
    '3': 'e',
    'b': '6',
    '6': 'b'
  }

  let output = '';
  for (let i = 0; i < str.length; i++) {
    let char = str.toLowerCase()[i];

    if (symbols[char]) {
      output += symbols[char]
    } else {
      output += char;
    }
  }
  return output;
}

const reverseCipher = (sentence) => {
  let words = sentence.split(' ');
  for (let i = 0; i < words.length; i++) {
    words[i] = words[i].split('').reverse().join('');
  }
   return words.join(' ');
};

module.exports = {
  symbolCipher,
  caesarCipher,
  reverseCipher
};


message-mixer.js

// Import the functions from encryptors.js here.
const encryptors = require('./encryptors.js');
// Encryption Functions
/////////////////////////////////////////////
const {caesarCipher, symbolCipher, reverseCipher} = encryptors;


// User Input / Output Logic
/////////////////////////////////////////////

const encryptionMethod = getEncryptionMethod();
process.stdin.on('data', (userInput) => {
  displayEncryptedMessage(encryptionMethod, userInput);
});

/* Helper function for determining which cipher method
the user chose when they ran the program. */
function getEncryptionMethod() {
  let encryptionMethod;
  
  const encryptionType = process.argv[2];  
  if (encryptionType === 'symbol') {
    encryptionMethod = symbolCipher;
  } else if (encryptionType === 'reverse') {
    encryptionMethod = reverseCipher;
  } else if (encryptionType === 'caesar') {
    let amount = Number(process.argv[3]);
    if (Number.isNaN(amount)) {
      process.stdout.write(`Try again with a valid amount argument. \n`)
      process.exit();  
    }
    encryptionMethod = (str) => caesarCipher(str, amount);
  } 
  else {
    process.stdout.write(`Try again with a valid encryption type. \n`)
    process.exit();
  }

  process.stdout.write('Enter the message you would like to encrypt...\n> ');
  return encryptionMethod;
}

/* Helper function for displaying the encrypted message to the user. */
function displayEncryptedMessage(encryptionMethod, userInput) {
  let str = userInput.toString().trim();    
  let output = encryptionMethod(str);
  process.stdout.write(`\nHere is your encrypted message:\n> ${output}\n`)
  process.exit();
}

If you know the error is in the decode() function, have you tried debugging?

Try setting up a trail of console logs and verify that everything is working the way you intend (have a few test cases for different types of input).

Thank you so much for this. Could you point out any Codecademy resources about debugging with JS (or any external resources about this topic)?

I did some debugging with Java, but I have never done that with JS.

Hi,

For debugging I highly recommend running your code locally on vs code or something like that.

I don’t know many debugging tips sites but in general here are some good guidelines:

  • outline what your functions do on paper (what type of input they take in, what they output)
  • isolate your functions and try different types of inputs.
  • put checkpoints in each one with informative console logs (using template literals to make it more readable): Template literals (Template strings) - JavaScript | MDN
  • If something unexpected happens in this process (something doesn’t behave how you expect), refer to the MDN documentation for more info about the method you are using to make sure it behaves truly as you expect.
1 Like

Thank you for the general tips! I just ran the code on VS Code (not on the Codecademy terminal), and I ran into the following error upon typing in npm test:

npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path C:\Users\e.kawamura\Desktop\message_mixer\package.json
npm ERR! errno -4058
npm ERR! enoent ENOENT: no such file or directory, open 'C:\Users\e.kawamura\Desktop\message_mixer\package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent 

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\e.kawamura\AppData\Roaming\npm-cache\_logs\2021-09-07T14_28_35_074Z-debug.log

I suppose I need to create package.json, but could you point out what kind of information I could type in? I found this Stackoverflow post, but this gives the OP errors still (according to the OP).

On a different note, I tried node super-encoder.js decode on VS Code and it worked out this time:

PS C:\Users\e.balenzela\Desktop\message_mixer> node super-encoder.js decode
Enter the message you would like to encrypt...
> urrkn jrxuc
hello world

So my code seems to function just fine although my debug failed.

Ah sorry about this, I had thought you set-up your local environment in your computer already (that’s another story).

Same process should be able to debug from codecademy environment though.