Message Mixer: step 6

Hi,
I’m doing the Message Mixer project in JavaScript intermediate course.

Here is what my message-mixer.js looks like

// Import the functions from encryptors.js here.
const encryptors = require('./encryptors.js');
const caesarCipher = encryptors.caesarCipher()
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();
}

Here is what my encryptors.js looks like

// Import the functions from encryptors.js here.
const encryptors = require('./encryptors.js');
const caesarCipher = encryptors.caesarCipher()
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();
}

And here is my bash code:

$ node message-mixer.js caesar 4
/home/ccuser/workspace/modules-message-mixer/encryptors.js:41
  for (let i = 0; i < str.length; i++) {
                         ^

TypeError: Cannot read property 'length' of undefined
    at Object.symbolCipher (/home/ccuser/workspace/modules-message-mixer/encryptors.js:41:26)
    at Object.<anonymous> (/home/ccuser/workspace/modules-message-mixer/message-mixer.js:4:33)
    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.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
    at startup (bootstrap_node.js:151:9)

I did search through the community but I found no solution for my problem. I would appreciate any advice for this.

1 Like

for encryptors.js you posted message-mixer.js again on accident I think.

but from the error message str does not exist(or is available) before line 41 in encryptors.js
if you need further help edit and post the code for encryptors.js

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

the code in encryptors.js was mistakenly pasted, I didn’t realize that though

Here is the link to the project: https://www.codecademy.com/courses/learn-intermediate-javascript/projects/message-mixer-node

Thank you.

@dqode @janbazant1107978602 I’ve edited my post!

I see a problem here:

You have some () that shouldn’t be there.

You are calling the functions by using () and its trying the run each function with no arguments / inputs.
However, you should be copying the functions (or a reference to each function) instead.

So

const caesarCipher = encryptors.caesarCipher()

should be

const caesarCipher = encryptors.caesarCipher

and so on …

3 Likes

Thank you for solving my issue. Have a good day!