Message Mixer Step 6 - Undefined error

Hello, I’m following along with the lession Message Mixer in the Node.JS lession under “intermediate Javascript” (https://www.codecademy.com/paths/web-development/tracks/webdev-intermediate-javascript/modules/intermediate-javascript-modules/projects/message-mixer-node) and I keep getting the same error when trying to pass step 6’s suggested text into the console: “node message-mixer.js caesar 4”

The error I get is in my encryptor.js file and is as follows:

$ node message-mixer.js caesar 4
/home/ccuser/workspace/modules-message-mixer/encryptors.js:63
module.exports.ceasarCipher = ceasarCipher
** ^**

ReferenceError: ceasarCipher is not defined
** at Object. (/home/ccuser/workspace/modules-message-mixer/encryptors.js:63:31)**
** at Module._compile (module.js:571:32)**
** at Object.Module._extensions…js (module.js:580:10)**
** at Module.load (module.js:488:32)**
** at tryModuleLoad (module.js:447:12)**
** at Function.Module._load (module.js:439:3)**
** at Module.require (module.js:498:17)**
** at require (internal/module.js:20:19)**
** at Object. (/home/ccuser/workspace/modules-message-mixer/message-mixer.js:2:20)**
** at Module._compile (module.js:571:32)**

I have read through similar problems with this lession and it seems that I’m doing everything right and I don’t understand how “ceasarCipher” in my encryptor.js file is undefined because it’s clearly defined as a function above.

Here are my files:
message-mixer.js:

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







// 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.ceasarCipher = ceasarCipher
module.exports.symbolCipher = symbolCipher
module.exports.reverseCipher = reverseCipher

and finally, super-encoder.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.ceasarCipher = ceasarCipher
module.exports.symbolCipher = symbolCipher
module.exports.reverseCipher = reverseCipher

Hey there!

Take a look at encryptors.js

Check out the name of its first function.

Then check out what you exported (and also, what you tried to import) :wink:

I’m sorry but I’m not seeing the difference. Copy and pasting side by side I get:
first function: caesarCipher
what I exported: ceasarCipher
what I imported: const ceasarCipher = encryptors.ceasarCipher

Precisely :slight_smile:

caesarCipher != ceasarCipher

3 Likes

Omg. My brain just saw this. You have no idea how many times I looke at these two. Literally side by side and couldn’t see a difference. Even after you just posted that I had to look like 4 times. Wow. Thank you.

2 Likes