Message Mixer Step 6 (caesarCipher is not a function?)

Exercise link: https://www.codecademy.com/courses/introduction-to-javascript/projects/message-mixer-node

Gonna need some help, please!
I tried to follow the guideline but I’m having an error message while typing the following commands:

$ node message-mixer.js caesar 4
cnawp fkx!

Error message shows something that I didn’t edit before.

/home/ccuser/workspace/modules-message-mixer/message-mixer.js:35
      caesarCipher(str, amount);
      ^

TypeError: caesarCipher is not a function

Here is my code on message mixer.js

// Import the functions from encryptors.js here.

const encryptors = require('./encryptors.js');

// Encryption Functions

/////////////////////////////////////////////

const caesarCipher = require.caesarCipher;

const symbolCipher = require.symbolCipher;

const reverseCipher = require.reverseCipher;

// 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();

}

Here is my code on 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(' ');

};

// Exports as module

module.exports.caesarCipher = caesarCipher;

module.exports.symbolCipher = symbolCipher;

module.exports.reverseCipher = reverseCipher;

My Bash code:

$ node message-mixer.js caesar 4
Enter the message you would like to encrypt...
> cnawp fkx!
/home/ccuser/workspace/modules-message-mixer/message-mixer.js:35
      caesarCipher(str, amount);
      ^

TypeError: caesarCipher is not a function
    at encryptionMethod (/home/ccuser/workspace/modules-message-mixer/message-mixer.js:35:7)
    at displayEncryptedMessage (/home/ccuser/workspace/modules-message-mixer/message-mixer.js:50:16)
    at ReadStream.process.stdin.on (/home/ccuser/workspace/modules-message-mixer/message-mixer.js:15:3)
    at emitOne (events.js:96:13)
    at ReadStream.emit (events.js:191:7)
    at readableAddChunk (_stream_readable.js:178:18)
    at ReadStream.Readable.push (_stream_readable.js:136:10)
    at TTY.onread (net.js:561:20)
$ ^C

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

Why do you declare ‘encryptors’ here? You do not seem to use it anywhere:

And why do you use such a different syntax here:

In the first example, you use ‘require’ as a function when you call it with parenthesis and in the second example, you use it as an object with dot notation.
Here you can find examples of how to use the ‘require’ function:

1 Like

Oh! Thanks! I can’t even notice my syntax was wrong. :exploding_head:

1 Like