Message Mixer -Intermediate JavaScript

CAN SOMEONE TAKE A LOOK AND OFFER SOME HELP AS TO WHY I CAN’T GET
STEP 11 TO FUNCTION. super-encoder,js has functionality for caesar 5, reverse and symbol but not for
ebcode and decode. If in console i input - node super-encoder.js encode or node super-encoder.js decode I receive the error message ‘TRY AGAIN WITH A VALID ENCRYPTION TYPE’ which is the expected function error message, but I don’t undersstand my error, omission? type? Please help.

//super-encoder.js
// Import the encryptors functions here.
const encryptors = require(’./encryptors.js’);

const caesarCipher = encryptors.caesarCipher;
const symbolCipher = encryptors.symbolCipher;
const reverseCipher = encryptors.reverseCipher;

const encodeMessage = (str) => {
// Use the encryptor functions here.
return reverseCipher(symbolCipher(caesarCipher(str, 6)));
}

//console.log(encodedMessage(‘lemon’));
const decodeMessage = (str) => {
// Use the encryptor functions here.
return caesarCipher(symbolCipher(reverseCipher(str)), -6);
}

// User input / output.

const handleInput = (userInput) => {
const str = userInput.toString().trim();
let output;
if (process.argv[2] === ‘encode’) {
output = encodeMessage(str);
}
if (process.argv[2] === ‘decode’) {
output = decodeMessage(str);
}

process.stdout.write(output + ‘\n’);
process.exit();
}

// Run the program.
process.stdout.write('Enter the message you would like to encrypt…\n> ');
process.stdin.on(‘data’, handleInput);

When I run super-encoder.js my output in console is:
$ node message-mixer.js reverse
Enter the message you would like to encrypt…

so this file has functionality for caesar 5, reverse and symbol but not for encode and decode

Here is your encrypted message:

os siht elif sah ytilanoitcnuf
$ node encryptors.js reverse
Enter the message you would like to encrypt…
and this file has functionality

Here is your encrypted message:

dna siht elif sah ytilanoitcnuf
$ node super-encoder.js encode
Try again with a valid encryption type.
$ node super-encoder.js reverse
Enter the message you would like to encrypt…
Enter the message you would like to encrypt…
But this file only has the functionality of message-mixer.js and/or ecryptors.js

Here is your encrypted message:

tuB siht elif ylno sah eht ytilanoitcnuf fo sj.rexim-egassem ro/dna sj.srotpyrce
$ super-encoder.js has access to the functions from message-mixer.js but not to its own functions???
bash: super-encoder.js: command not found
$

//HERE IS THE ENCRYPTORS.JS FILE CONTENT
//AS INSTRUCTED THE 3 FUNCTIONS FROM MESSAGE-MIXER.JS HAVE BEEN TRANSFERRED 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(’ ');
};

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

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

//HERE IS THE MESSAGE-MIXER.JS FILE AFTER MODIFICATION

// Import the functions from encryptors.js here.
const encryptors = require(’./encryptors.js’);

//option - object destructuring
const { caesarCipher, symbolCipher, reverseCipher } = encryptors;

//option - named function
//onst caesarCipher = encryptors.caesarCipher;
//const symbolCipher = encryptors.symbolCipher;
//const reverseCipher = encryptors.reverseCipher;

// Encryption Functions
/////////////////////////////////////////////
//now moved to encryptors.js and deleted here

// User Input / Output Logic
/////////////////////////////////////////////

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

In the Message Mixer project, you’re writing the program so that

these should work for message-mixer.js :

node message-mixer.js caesar 5
node message-mixer.js reverse
node message-mixer.js symbol

these should work for super-encoder.js :

node super-encoder.js encode
node super-encoder.js decode

Which ones work?
Which ones don’t work?

And don’t forget to format each piece of code in the post using the </> button to make it easier to read.
(or you can put blocks of code between ``` and ``` to do the same thing).

//These three work
node message-mixer.js caesar 5
node message-mixer.js reverse
node message-mixer.js symbol

//These two do not work
node super-encoder.js encode
node super-encoder.js decode

//Error Message
$ node super-encoder.js encode
Try again with a valid encryption type.

You have this code in encryptors.js (lines 65 - 68) just before the function getEncryptionMethod

const encryptionMethod = getEncryptionMethod();
process.stdin.on('data', (userInput) => {
  const encryptionMethod = getEncryptionMethod();
  displayEncryptedMessage(encryptionMethod, userInput);
});

move that code to message-mixer.js, just before the the function getEncryptionMethod in that file.

(I think this code was running for when you did the require('./encryptors.js') in super-encoder.js, but its only for message-mixer.js)

That was precisely the issue.
But I don’t find a step in the instruction where that is addressed, did I miss it?
Thanks so much.

FYI: To anyone reading this:
I noticed that super-encoder,js also has access to the functionality of caesar 5, reverse and symbol from message-mixer.js, in addition to it’s own functions: encode and decode

//Before moving encryptionMethod from encryptors.js to message-mixer.js.
$ node super-encoder.js encode
Try again with a valid encryption type.

//After moving code block.
$ node super-encoder.js encode
Enter the message you would like to encrypt…

wellthisisgreat
zgkxmy0y0nzrrkc
$ node super-encoder.js decode
Enter the message you would like to encrypt…
wellthisisbetter
l3nn36$!$!bn113q

I think it was addressed in the step that said to move the encryption functions from message-mixer.js to encrytors.js
they wanted you to move only the 3 functions: caesarCipher, symbolCipher, reverseCipher to encryptors.js and leave the other code in place.

Yes, now I recall what I did. Initially I did move the encryptionMethod logic from encryptors.js to message-mixer.js, but when I couldn’t get super-encoder.js to work, among other things while troubleshooting, I deleted it.
Then, I modified the import/export statements which fixed super-encoder.js, but now it wasn’t functional due to the
missing logic for encryptionMethod, which you noticed. So returning it to message-mixers.js made all things right.
I just wanted to retrace and understand what I did correctly and incorrectly.
Thanks again.

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