Help with Step 9 in Message Mixer

Hi I am a new coder and I am strugguling with this problem, could sombody help me telling me what I am missing?

I am having trouble with the step 9 I think it should run but it is still showing as undefined,

The step is the following:
Use the three encryption methods to complete the encodeMessage() function by passing the input str parameter into one encryption function and then passing the returned value to the next encryption function. encodeMessage() should return the final encoded message.

Some guidelines:

  • You must use each encryption function at least once.
  • It is your choice what order you use to call the encryption functions.
  • You may also re-use encryption functions to encode the message.
  • caesarCipher() requires an amount argument. Choose some number between 0 and 26 to use for this argument and remember your choice for decodeMessage() .

When you are finished, try running the program like so:
$ node super-encoder.js encode

The code writen is the followig

//messsage-mixer

{
// Import the functions from encryptors.js here.

const encryptors = require('./encryptors.js');

const caesarCipher = encryptors.caesarCipher;

const symboCipher = encryptors.symboCipher;

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

}
}

//encryptor.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;
}

//super-encoder

{
// Import the encryptors functions here.
const encryptors = require('./encryptors.js');

const caesarCipher = encryptors.caesarCipher;
const symboCipher = encryptors.symboCipher;
const reverseCipher = encryptors.reverseCipher;

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

const decodeMessage = (str) => {
  // Use the encryptor functions here.
  
}

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

I’m having the same problem, did you ever figure out how to fix it?

If you’re still having problems, I did notice that you’ve written “symboCipher” instead of “symbolCipher” (notice the “l” before “C” and after “o”). But, I don’t know if that will resolve your issue.

1 Like

Thanks for the observation, I just changed it and it’s still returning undefined, good observation though…

Hi @board9685990051 just solved it,

The error was because I was writing

return 
    reverseCipher(symbolCipher(caesarCipher(str,6)));

Innsted of

return reverseCipher(symbolCipher(caesarCipher(str,6)));

Hope it helps you to.

6 Likes

Brilliant! Thanks a lot! :grinning:

Hey i’m a bit confused, could you explain to me what this code is actually doing? Thanks in advance.

1 Like

It is not clear if you are asking why the code was breaking with the dual line option vs the solved single line response, or if you want to understand why there are multiple cypher calls.
If the first: Because of the Automatic Semicolon Insertion in JS. In JS an empty return line is automatically terminated with a ; as explained here:

If the second: This multi cypher call is part of the Encode Decode exercise of the learning module. A piece of text is being “encrypted” or encoded by passing it through a series of multiple cipher algorithms provided. Another method does the decoding of the encrypted character by running the reverse cipher steps to get back the original Text.

1 Like

there is no difference between them 2 lines :open_mouth:

Thanks @ivan-avila it was the first case my issue and yes I solve it by removing the space to remove the automatic semicolon.

1 Like

Hi joearre and the rest of coders.
Just like you I am still stuck at this step on this project so I created a post to see if somebody can give a bit of help with my issue: by the way there are many students stuck on step 10 and 11 too. With no CODE SOLUTION or Video Tutorial explaining how the solution works we are in the deep mud

Thank you in advance!

Junier

Hey, @joearre thanks for the clarification.

Thank you. This is very helpful.

thanks I to had a similar error

Thank you! You would think that the help examples would show that the code needs to be on the same line as the return!