Message Mixer step 6: can't read property length of undefined

https://www.codecademy.com/paths/readytrack-jul22/tracks/basics-of-be-development-WGU/modules/wdcp-22-modular-development-with-node-js/projects/message-mixer-node

Hi everyone,

I’m stuck on step 6. The error I’m getting on my bash is:

node message-mixer.js caesar 4

/home/ccuser/workspace/modules-message-mixer/encryptors.js:7
  for (let i = 0; i < str.length; i++) {
                         ^

TypeError: Cannot read property 'length' of undefined
    at caesarCipher (/home/ccuser/workspace/modules-message-mixer/encryptors.js:7:26)
    at Object.<anonymous> (/home/ccuser/workspace/modules-message-mixer/encryptors.js:61:31)
    at Module._compile (module.js:571:32)
    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.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/ccuser/workspace/modules-message-mixer/message-mixer.js:2:20)

The code for the function was copy and pasted over from the message-mixer file and put into the encryptors file, so I didn’t write any of this out, which makes a bit confused why it’s not working.

file code below:
message-mixer.js:

// Import the functions from encryptors.js here.
const encryptors = require('./encryptors.js')
const {caesarCipher, symbolCipher, reverseCipher} = encryptors
// 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();
}

encryptors.js:

// Declare and export the functions here.
function 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;
}

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

 function 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.js:

// Import the encryptors functions here.


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

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

I’ve spotted one issue at the end of encryptors.js

You’re exporting the result of calling caesarCipher, not the ceasarCipher function.
So
module.exports.caesarCipher = caesarCipher();
should be
module.exports.caesarCipher = caesarCipher;
and so that you export the function instead.
And the same for the others.

2 Likes

OMG thank you so much, truly appreciated, I don’t know if I would have ever found that. rest of the project went smoothly.