Message Mixer - Intermediate JavaScript Modules Project Step 9

I’m following the instructions and my code appears correct. However, it throws an error once I run the program in Codecademy’s built-in terminal (see pic). So do I assume this is normal because I am not finished with the lesson? I don’t want to continue if there’s an issue I need to fix but I really don’t want to wait and feel stuck not knowing where to go from here.

Check if you exported caesarCipher and the other functions in encryptors.js

You should have something like:

module.exports.caesarCipher = caesarCipher;
module.exports.symbolCipher = symbolCipher;
module.exports.reverseCipher = reverseCipher;

at the end of encryptors.js

Yes I have all three functions exported in the encryptors.js. Maybe it’s my syntax?

I added the following at the end of the code. At first I wasn’t sure if they were suppose to be inside each function block but either way seemed to yield the same error.

module.exports.caesarCipher = caesarCipher;
module.exports.symbolCicpher = symbolCipher;
module.exports.reverseCipher = reverseCipher;

What is your code for caesarCipher and the other functions? Maybe the problem is there.

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

those functions all seem to work correctly

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

Thank you for helping. I guess I’ll continue on and see if adding code will correct it. Don’t know what else to do!

Could it be something in the code of the message-mixer.js?

const encryptors = require(‘./encryptors.js’);

const {caesarCipher, symbolCipher, reverseCipher} = encryptors;

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

Trying again. This time it says the symbolCipher is not a function at encodeMessage. Not sure what to do moving forward. Any help is appreciated.

Found the solution! It’s in the way the module.exports as an object in task #4 should be written. I changed how I had it as suggested in the link below and it worked.

link to topic