Message Mixer step 10

Link: https://www.codecademy.com/courses/learn-intermediate-javascript/projects/message-mixer-node
Hey, I’m trying to do the message mixer project and I’m getting stuck on step 10. When I try to decode the message, I get a ReferenceError that says caesarCipher isn’t a function. I don’t know why, as encoding works perfectly fine.
It’s saying that the error is in encryptors.js, which is the file that’s already written for you when you do the project.
Error:

/home/ccuser/workspace/modules-message-mixer/encryptors.js:8
    return caesarCipher(str, amount + 26);
           ^

ReferenceError: caesarCipher is not defined
    at module.exports.caesarCipher (/home/ccuser/workspace/modules-message-mixer/encryptors.js:8:12)
    at decodeMessage (/home/ccuser/workspace/modules-message-mixer/super-encoder.js:11:36)
    at ReadStream.handleInput (/home/ccuser/workspace/modules-message-mixer/super-encoder.js:23:14)
    at emitOne (events.js:96:13)
    at ReadStream.emit (events.js:191:7)
    at readableAddChunk (_stream_readable.js:178:18)
    at ReadStream.Readable.push (_stream_readable.js:136:10)
    at TTY.onread (net.js:561:20)

Code in encryptors.js:

// Declare and export the functions here.

// Encryption Functions
/////////////////////////////////////////////

module.exports.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;
};

module.exports.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;
}

module.exports.reverseCipher = (sentence) => {
  let words = sentence.split(' ');
  for (let i = 0; i < words.length; i++) {
    words[i] = words[i].split('').reverse().join('');
  }
   return words.join(' ');
};

I think the problem is that on line 8 of encryptors.js inside the function, it’s saying to return the function, if that makes sense.
return caesarCipher(str, amount + 26);
Anyone know how to fix this?

1 Like

Hello!

Your caesarCipher function does not exist in the module’s namespace, so you cannot use it like caesarCipher(str, amount + 26);.
One solution is to define function as const caesarCipher = ...; and then export that constant module.exports.caesarCipher = caesarCipher;. So caesarCipher const will exist in the module’s namespace and can be used like caesarCipher(str, amount + 26);.

2 Likes

Finally, someone explaining this correctly

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