JAVASCRIPT SYNTAX, PART III Message Mixer

Hi codecademy community i try to fix a problem to import a three function from file “encryptors.js” to the file “message-mixer.js” in the same folder i do it this step by import all these functions and i had store it in variable called encryptors but when i try to execute the caeser function from the encryptors the code don’t run well and this is the error displayed in the console " node message-mixer.js caesar 4 /home/ccuser/workspace/modules-message-mixer/message-mixer.js:6 const caesarCipher = Cipher.caesarCipher(); ^ SyntaxError: Identifier 'caesarCipher' has already been declared at createScript (vm.js:53:10) at Object.runInThisContext (vm.js:95:10) at Module._compile (module.js:543:28) 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) node message-mixer.js caesar 4
/home/ccuser/workspace/modules-message-mixer/message-mixer.js:6
const caesarCipher = Cipher.caesarCipher();
^
SyntaxError: Identifier ‘caesarCipher’ has already been declared
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
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)" and this is my code` *The code in the encryptors.js file// Declare and export the functions here.const caesarCipher = (str, amount = 0) => {

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

The code in the message-mixer.js" filehttps://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-iii/modules/fscp-learn-javascript-syntax-modules/projects/message-mixer-node : // Import the functions from encryptors.js here.
const encryptors = require(’./encryptors.js’);
const {caesarCipher, symbolCipher, reverseCipher} = Cipher;
// Encryption Functions
/////////////////////////////////////////////
const caesarCipher = Cipher.caesarCipher;
const symbolCipher = Cipher.symbolCipher;
const reverseCipher = Cipher.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();
}

``Preformatted text`

Hi @method3884535642
this post is extremely difficult to read. Please use the editor’s code formatting button </> to mark your code.

This seems to be in the same file. Here you declared caesarCipher for the first time: const {caesarCipher, symbolCipher, reverseCipher}, and here is the second declaration: const caesarCipher = Cipher.caesarCipher; which causes the error.

thank you a lot my friend for your help.

my friend i want to participate to help other people and to have more challenges in the codecademy forums how can i do to access this

What do you mean exactly? You’re already in the forum and free to answer other people’s questions…

yes of course i want to improve my skills in programation

Here is what works best for me: Do a mix of lessons, projects from Codecademy and your own projects. When you encounter errors, don’t worry. Debugging is the main part of programming and boosts your progress. I guess the line I’ve written most in JS is console.log.
When/before you post a topic, try to narrow down your problem as far as possible. Log variables, google error messages, format your code and structure your posts. Describe what you’ve done to find the error as exactly as possible. That helps others to provide solutions, but it also helps you to understand what you’ve done (wrong).
Read other people’s posts, do some research and try to provide answers with helpful resources.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.