Message Mixer project, step 8 error

Hi all, I am getting the following error when I try to run the command: node super-encoder.js encode

/home/ccuser/workspace/modules-message-mixer/super-encoder.js:6
const {}
^^
SyntaxError: Missing initializer in destructuring declaration

My exact same code ran previously when I ran message-mixer.js.
line 6 refers to my restructuring declaration.

// Import the encryptors functions here.

const {caesarCipher,symbolCipher,reverseCipher} = require('./encryptors.js');

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

Can’t figure out what I’m missing. :slightly_frowning_face:

I went ahead and finished the program. Now the error message does not appear and the code runs as expected. :thinking:

Ye it looks weird, just for curiosity, line 6 of your code what was exactly?

1 Like
const {caesarCipher,symbolCipher,reverseCipher} = require('./encryptors.js');

Shouldn’t that be the first line of code?
I think this problem can happen only if the arguments inside the curly brackets are not exactly the same as the properties of the object you’re destructuring. If you’re interested to review this concept I personally found this thread on Codeacademy very useful thanks to all the examples there are throughout its length. :slight_smile:

Shouldn’t that be the first line of code?

It is my first line of code. There are some comments above it that I neglected to delete.

Here’s my code from encryptors.js, the arguments match.

module.exports = {
  caesarCipher,
  symbolCipher,
  reverseCipher
}
1 Like

Then I’d speculate that it didn’t save when/if you wrote the destructuring.
Running node super-econder.js, if the file was not saved, then it would be calling the last saved version of the file not considering latest modifications. Probably working on it, at some point it got saved and testing again the issue seemed to have disappeared.

1 Like

It said SyntaxError so something might be wrong in your code, I guess. Try to make it assign to a variable instead →

const encrypHere = require("./encryptors.js");

const { caesarCipher, symbolCipher, reverseCipher } = encrypHere;

Then run this example that is given →

$ node message-mixer.js caesar 4
Enter the message you would like to encrypt...
> hello world
 
Here is your encrypted message:
> lipps asvph
 
$ node message-mixer.js 'reverse'
Enter the message you would like to encrypt...
> hello world
 
Here is your encrypted message:
> olleh dlrow

That makes sense. Thank you for your help.

1 Like

Thank you for your input. However, I already tried your suggestion (didn’t work) :grimacing:

Would you mind sharing your whole source code here?

encryptors.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,
  symbolCipher,
  reverseCipher
}

super-encoder.js

// Import the encryptors functions here.

const {caesarCipher,symbolCipher,reverseCipher} = require('./encryptors.js');

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

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);`
type or paste code here

message-mixer.js

// Import the functions from encryptors.js here.

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

const encryptors = require('./encryptors.js');
const {caesarCipher,symbolCipher,reverseCipher}
= encryptors;

In message-mixer.js, I think this might be the problem. JavaScript can’t detect the logic from which file if you imported it at the bottom. From what I knew, JavaScript runs the code from top to bottom.

Okay. You may be on to something. However my code ran as expected and I completed the assignment without further problems. And I appreciate your effort.