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)