Message mixer step 6

hello i am on step 6 of the project message mixer and it returns an error after i type in “node message-mixer. js caesar 4” but i get an error

here is my code on message mixer.js

// Import the functions from encryptors.js here.
const encryptors = require('path/to/module.exports./encryptors.js');
// Encryption Functions
/////////////////////////////////////////////
const caesarCipher = object.caesarCipher;
const symbolCipher = object.symbolCipher;
const reverseCipher = object.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();
}

now here is my code on encryptors.js

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 ={
  caesarCipher, 
  symbolCipher, 
  reverseCipher
};

and finally here is my bash code

$ node messag-mixer.js caesar 4
module.js:472
    throw err;
    ^

Error: Cannot find module '/home/ccuser/workspace/modules-message-mixer/messag-mixer.js'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
    at startup (bootstrap_node.js:151:9)
    at bootstrap_node.js:542:3

I am not understanding what i did wrong in message-mixer.js and I would appreciate any advice for this.

Look closely at the error message:
Cannot find ... /messag-mixer.js
Do you have a file named messag-mixer.js?

okay. that was bad spelling, should have looked better. but now Bash has a new issue. did i put in the module wrong in message-mixer.js

here is the bash code and error.

$ node message-mixer.js caesar 4
module.js:472
    throw err;
Error: Cannot find module 'path/to/module.exports./encryptors.js'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/ccuser/workspace/modules-message-mixer/message-mixer.js:2:20)
    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)

Unless you have folders named path, to and module.exports, your path isn’t valid. I can’t see your file structure, but likely your encryptors.js file is in the same folder as message-mixer.js. If that is in fact the case, the relative path to encryptors.js would be './encryptors.js'.

one more question how do i delete bash code. i am trying to clear up clutter and bad code

Do you mean to clear the console? If so, you just type clear and hit <enter>.

If you are a Pro subscriber, you may want to check out this course:
https://www.codecademy.com/learn/learn-the-command-line

its still not working and i don’t understand why. it says there is something wrong with the module but i can’t figure out what. why is this the one lesson without a video?

here is the file structure:

files

cnawp

encryptors.js

message-mixer.js

super-encoder.js

here is the bash code and error

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

Error: Cannot find module '/encryptors.js'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/ccuser/workspace/modules-message-mixer/message-mixer.js:2:20)
    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)
$ 

I don’t work for CC, so I can’t answer that.

If you post your updated code from encryptors.js and message-mixer.js, I or someone else may be able to assist you.

here is the code for message mixers

// Import the functions from encryptors.js here.
const encryptors = require('./encryptors.js');
// Encryption Functions
/////////////////////////////////////////////
const caesarCipher = object.caesarCipher;
const symbolCipher = object.symbolCipher;
const reverseCipher = object.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 the code for encryptors

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 = {
  caesarCipher, 
  symbolCipher, 
  reverseCipher
};

Can you post a screenshot of the file explorer. (Click on the folder icon on the top left of the code editor.) Should look like this:
image

here

Screenshot (2)

That looks okay. Try clearing the bash console once again, and then type ls and hit <enter>. (That’s a lowercase L not a 1 followed by lowercase S.) Then type pwd, and hit <enter>, and post a screenshot.
Should look like this:
image

here

That all looks correct. Are you sure you saved your code after updating this line:

I copied, and ran your code in place of my own, and the module was found with no issues. You do have another issue here:

But you shouldn’t have the Cannot fine module error anymore.

so then do you happen to know the issue? all i did with the objects was follow directions as they were given to me.

Looks like you took the Hint a little too literally. :wink:
You imported an object, and assigned it to a variable.

You need to use the name of the variable in place of the word object in the lines I referenced.

thanks for the assist its working.

1 Like

A post was split to a new topic: Question about importing modules