Message Mixer updated version

Hello codecademy-community!,

i wanted to complete the task 6 and run into an error, could someone please, explain and show me where i did the mistake. The task / the file of message-mixer.js/ encryptors.js and the error code is down below:

Task 6:
Well done! If you’ve completed the steps correctly you should now be able to run the program again. For example, try running this command in the terminal:

node message-mixer.js caesar 4

And then, when prompted, type the following message:

> cnawp fkx!

By moving the encryption functions into a separate module and exporting them, you’ve separated the concerns of the program, making it more organized and easier to navigate. Furthermore, you can now use these encryption methods to build another program!

message-mixer.js

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

const encryptorsA = encryptors.caesarCipher;
const encryptorsB = encryptors.symbolCipher;
const encryptorsC = encryptors.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();
}
encryptor.js
// Declare and export the functions here.

module.exports.caesarCipher = () => {};
module.exports.symbolCipher = () => {};
module.exports.reverseCipher = () => {};

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(' ');
};

Error code:
$ node message-mixer.js caesar 4
module.js:472
throw err;
^

Error: Cannot find module ‘encryptors.js’
at Function.Module._resolveFilename (mo
dule.js:470:15
at Function.Module._load (module.js:418
:25)

on the import path, i missed something and added it, but it still doesn’t work.
The new error says: ReferenceError: caesarcipher is not defined at encryptenMethod(message-mixer.js: 34:33.
Does anyone know what it means and where i can change it?

Looks like a typo to me?

Should that be encryptionMethod?

Hey I am also getting the same issue here,

for my message-mixer.js file;

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

In the encryptors file



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.functionToExport = caesarCipher;
module.exports.functionToExport = symbolCipher;
module.exports.functionToExport = reverseCipher;






Following Error is thrown

$ 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:34
    encryptionMethod = (str) => caesarCipher(str, amount);
                                ^

TypeError: caesarCipher is not a function
    at encryptionMethod (/home/ccuser/workspace/modules-message-mixer/message-mixer.js:34:33)
    at displayEncryptedMessage (/home/ccuser/workspace/modules-message-mixer/message-mixer.js:48: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)
$ 

Hi There,

In your encryptors file, your module.exports are incorrect. FunctionToExport should be the name of the function you are exporting.

I am working on this module after the big change and blowing off the past few weeks of working on the course. I tried both ways shown above and it’s complaining about caesarCipher with: ReferenceError: caesarcipher is not defined at object/)/home…/message-mixer/encryptors.js:9:3) In ecryptors.js I have: at lines 8-10:
module.exports = {

caesarCipher, symbolCipher,reverseCipher

};

in message-mixer.js in my import section i have:
const encryptors = require(’./encryptors.js’)

and:
const { caesarCipher, symbolCipher, reverseCipher } = encryptors;

I also tried using:
const encryptorFunctionA = encryptors.caesarCipher;
const encryptorFunctionB = encryptors.symbolCipher;
const encryptorFunctionC = encryptors.reverseCipher;

to no avail. I am stumped at the moment but will continue looking at this. I just commented out the functions in message-mixer.js and plugged in the import stuff and in encryptors.js used the above code as shown by the other two folks having issues with it.

Any ideas?

OK made some headway, in encryptors.js i changed the code for declare and export to be module.exports.nameOfCipher = () => {} for the 3 ciphers where nameOf is each of the 3 Ciphers.

in message-mixer.js the import section has:
const encryptors = require(’./encryptors.js’);

In message-mixer.js the code iin the Encryption Funcions as: const caesarCipher = encryptors.caesarCipher; and added other 2 Ciphers below it.

Now the results of running it (no more reference issues) comes back as undefined.

Added a debug comment right below user input section and above:const encryptionMethod = getEncryptionMethod();
to log module.exports in console.log and it returns {} which I am not sure why it’s not displaying the 3 function calls that were exported (supposedly from encryptors.js file.

and getting same results from super-encryptor.js file. Inputs taken, just undefined as the result. So there is one thing i am missing here still.

1 Like

i struggled with this a lot as well the export statements put them at the end of encryptors.js

seems like when you put them at start it doesnt know about them

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.caesarCipher = caesarCipher;

module.exports.symbolCipher = symbolCipher;

module.exports.reverseCipher = reverseCipher;

Hi, did you manage to resolve this? How were you able to fix it as am having the same issue of undefined. Do you mind helping? Regards

The code required is as follows:-

At the bottom of the encryptors.js file, add :-
module.exports.caesarCipher = caesarCipher;
module.exports.symbolCipher = symbolCipher;
module.exports.reverseCipher = reverseCipher;

At the top of the message-mixer.js, add :-
const encryptors = require(’./encryptors.js’);
const { caesarCipher, symbolCipher, reverseCipher } = encryptors;

1 Like