Message Mixer

Hi im having an error message when I run my code, here’s the link to the exercise: https://www.codecademy.com/paths/web-development/tracks/webdev-intermediate-javascript/modules/intermediate-javascript-modules/projects/message-mixer

I’m currently at step 8 and im having this error message:

/home/ccuser/workspace/intermediate-javascript_messageMixer/message.js:4
console.log(MessageMixer.countCharacter(“What is the color of the sky?”, “t”));
^

TypeError: Cannot read property ‘countCharacter’ of undefined
at displayMessage (message.js:2:15)
at Object. (message.js:10:1)
at Module._compile (module.js:571:32)
at loader (/home/ccuser/node_modules/babel-register/lib/node.js:158:5)
at Object.require.extensions.(anonymous function) [as .js] (/home/ccuser/node_modules/babel-register/lib/node.js:168:7)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Function.Module.runMain (module.js:605:10)
at /home/ccuser/node_modules/babel-cli/lib/_babel-node.js:171:48

also here’s my code from messageMixer.js:
MessageMixer.countCharacter = function countCharacter(inputString, inputCharacter) {
let count = 0;
let string = inputString.toLowerCase();
let character = inputCharacter.toLowerCase();
for (let i = 0; i < string.length; i++) {
if (string[i] === character) {
count++;
}
}
return count;
};

MessageMixer.capitalizeFirstCharacterOfWords = function capitalizeFirstCharacterOfWords(string) {
let arr = string.split(" “);
for (let i = 0; i < arr.length; i++) {
let word = arr[i];
arr[i] = word[0].toUpperCase() + word.substring(1);
}
return arr.join(” ");
};

MessageMixer.reverseWord = function reverseWord(word) {
return word.split("").reverse().join("");
};

MessageMixer.reverseAllWords = function reverseAllWords(sentence) {
let words = sentence.split(" “);
for (let i = 0; i < words.length; i++) {
words[i] = MessageMixer.reverseWord(words[i]);
}
return words.join(” ");
};

MessageMixer.replaceFirstOccurence = function replaceFirstOccurence(string, toBeReplaced, replaceWith) {
return string.replace(toBeReplaced, replaceWith);
};

MessageMixer.replaceAllOccurrences = function replaceAllOccurrences(string, toBeReplaced, replaceWith) {
return string.split(toBeReplaced).join(replaceWith);
};

MessageMixer.encode = function encode(string) {
let replacementObject = { “a”: “@”, “s”: “$”, “i”: “!”, “o”:“0” };
for (let key in replacementObject) {
string = MessageMixer.replaceAllOccurrences(string, key, replacementObject[key]);
}
return string;
};

const MessageMixer = {};
displayMessage();
module.exports = MessageMixer;

and here’s the code from message.js:

function displayMessage() {
console.log(MessageMixer.countCharacter(“What is the color of the sky?”, “t”));
console.log(MessageMixer.capitalizeFirstCharacterOfWords(“What is the color of the sky?”));
console.log(MessageMixer.reverseWord(“What is the color of the sky?”));
console.log(MessageMixer.reverseAllWords(“What is the color of the sky?”));
console.log(MessageMixer.replaceFirstOccurence(“What is the color of the sky?”, “sky”, “water”));
console.log(MessageMixer.encode(“What is the color of the sky?”));
}

displayMessage();

const MessageMixer = require(’./messageMixer.js’);

This should be your first line of code instead of nearly last in your messageMixer.js file. The ‘undefined’ error is because the MessageMixer object isn’t being declared until after the functions are defined.

You’ll want to delete: displayMessage(); from your messageMixer.js file since the function is located in your message.js file. Also need to move const MessageMixer = require(’./messageMixer.js’); to the top of your message.js file.

For future posts, when you want to paste your code into your message, please click on the </> icon first, and then paste your code in the space indicated. It will preserve formatting, and make your code appear like this:

MessageMixer.countCharacter = function countCharacter(inputString, inputCharacter) {
let count = 0;
let string = inputString.toLowerCase();
let character = inputCharacter.toLowerCase();
for (let i = 0; i < string.length; i++) {
if (string[i] === character) {
count++;
}
}
return count;
};

Happy coding!

Hi thank you for your answer but unfortunately i still get this error message:

/home/ccuser/workspace/intermediate-javascript_messageMixer/messageMixer.js:3
MessageMixer.countCharacter = function countCharacter(inputString, inputCharacter) {
                            ^

TypeError: Cannot set property 'countCharacter' of undefined
    at Object.<anonymous> (messageMixer.js:1:1)
    at Module._compile (module.js:571:32)
    at loader (/home/ccuser/node_modules/babel-register/lib/node.js:158:5)
    at Object.require.extensions.(anonymous function) [as .js] (/home/ccuser/node_modules/babel-register/lib/node.js:168:7)
    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> (message.js:1:22)

here my code for message.js:

const MessageMixer = require('./messageMixer.js');

function displayMessage() {
  console.log(MessageMixer.countCharacter("What is the color of the sky?", "t"));
  console.log(MessageMixer.capitalizeFirstCharacterOfWords("What is the color of the sky?"));
  console.log(MessageMixer.reverseWord("What is the color of the sky?"));
  console.log(MessageMixer.reverseAllWords("What is the color of the sky?"));
  console.log(MessageMixer.replaceFirstOccurence("What is the color of the sky?", "sky", "water"));
  console.log(MessageMixer.encode("What is the color of the sky?"));
}

displayMessage();

and for messageMixer:

MessageMixer.countCharacter = function countCharacter(inputString, inputCharacter) {
  let count = 0;
  let string = inputString.toLowerCase();
  let character = inputCharacter.toLowerCase();
    for (let i = 0; i < string.length; i++) {
      if (string[i] === character) {
         count++;
      }
    }
  return count; 
};

MessageMixer.capitalizeFirstCharacterOfWords = function capitalizeFirstCharacterOfWords(string) {
  let arr = string.split(" ");  
    for (let i = 0; i < arr.length; i++) {  
      let word = arr[i];
        arr[i] = word[0].toUpperCase() + word.substring(1); 
    }
  return arr.join(" "); 
};


MessageMixer.reverseWord = function reverseWord(word) {
  return word.split("").reverse().join("");
};

MessageMixer.reverseAllWords = function reverseAllWords(sentence) {
  let words = sentence.split(" ");
    for (let i = 0; i < words.length; i++) {
      words[i] = MessageMixer.reverseWord(words[i]);
    }
   return words.join(" ");
};


MessageMixer.replaceFirstOccurence = function replaceFirstOccurence(string, toBeReplaced, replaceWith) {
  return string.replace(toBeReplaced, replaceWith);
};


MessageMixer.replaceAllOccurrences = function replaceAllOccurrences(string, toBeReplaced, replaceWith) {
  return string.split(toBeReplaced).join(replaceWith);
};

MessageMixer.encode = function encode(string) {
  let replacementObject = { "a": "@", "s": "$", "i": "!", "o":"0" };
    for (let key in replacementObject) {
      string = MessageMixer.replaceAllOccurrences(string, key, replacementObject[key]); 
    }	
    return string;
};

const MessageMixer = {};
module.exports = MessageMixer;```

This line still needs to be at the top of your messageMixer.js file before any of your functions are defined.

Thank you! it worked :slight_smile: :love_you_gesture:t6:

1 Like

I’m at step 12 now and encountered yet another error message :confused:.

the error message goes as follow:

/home/ccuser/workspace/intermediate-javascript_messageMixer/message.js:6
  console.log(MessageMixer.countCharacter("What is the color of the sky?", "t"));
                           ^

TypeError: MessageMixer.countCharacter is not a function
    at displayMessage (message.js:5:28)
    at Object.<anonymous> (message.js:13:1)
    at Module._compile (module.js:571:32)
    at loader (/home/ccuser/node_modules/babel-register/lib/node.js:158:5)
    at Object.require.extensions.(anonymous function) [as .js] (/home/ccuser/node_modules/babel-register/lib/node.js:168:7)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Function.Module.runMain (module.js:605:10)
    at /home/ccuser/node_modules/babel-cli/lib/_babel-node.js:171:48

here’s my code for message.js:

const MessageMixer = require('./messageMixer.js');

import MessageMixer from './messageMixer'

function displayMessage() {
  console.log(MessageMixer.countCharacter("What is the color of the sky?", "t"));
  console.log(MessageMixer.capitalizeFirstCharacterOfWords("What is the color of the sky?"));
  console.log(MessageMixer.reverseWord("What is the color of the sky?"));
  console.log(MessageMixer.reverseAllWords("What is the color of the sky?"));
  console.log(MessageMixer.replaceFirstOccurence("What is the color of the sky?", "sky", "water"));
  console.log(MessageMixer.encode("What is the color of the sky?"));
}

displayMessage();

and for messageMixer.js

const MessageMixer = {};

MessageMixer.countCharacter = function countCharacter(inputString, inputCharacter) {
  let count = 0;
  let string = inputString.toLowerCase();
  let character = inputCharacter.toLowerCase();
    for (let i = 0; i < string.length; i++) {
      if (string[i] === character) {
         count++;
      }
    }
  return count; 
};

MessageMixer.capitalizeFirstCharacterOfWords = function capitalizeFirstCharacterOfWords(string) {
  let arr = string.split(" ");  
    for (let i = 0; i < arr.length; i++) {  
      let word = arr[i];
        arr[i] = word[0].toUpperCase() + word.substring(1); 
    }
  return arr.join(" "); 
};


MessageMixer.reverseWord = function reverseWord(word) {
  return word.split("").reverse().join("");
};

MessageMixer.reverseAllWords = function reverseAllWords(sentence) {
  let words = sentence.split(" ");
    for (let i = 0; i < words.length; i++) {
      words[i] = MessageMixer.reverseWord(words[i]);
    }
   return words.join(" ");
};


MessageMixer.replaceFirstOccurence = function replaceFirstOccurence(string, toBeReplaced, replaceWith) {
  return string.replace(toBeReplaced, replaceWith);
};


MessageMixer.replaceAllOccurrences = function replaceAllOccurrences(string, toBeReplaced, replaceWith) {
  return string.split(toBeReplaced).join(replaceWith);
};

MessageMixer.encode = function encode(string) {
  let replacementObject = { "a": "@", "s": "$", "i": "!", "o":"0" };
    for (let key in replacementObject) {
      string = MessageMixer.replaceAllOccurrences(string, key, replacementObject[key]); 
    }	
    return string;
};

MessageMixer.palindrome = function palindrome(str){
  return `${str} ${messageMixer.reverseWord(str)}`
}

MessageMixer.pigLatin = function pigLatin(sentence, character){
  return sentence.split(' ').join(character + ' ')
}

export default MessageMixer;```

Not sure if this is the only issue yet, but the line I mention in the comment to your code above needs to be commented out or deleted. You cannot have a duplicate declaration of MessageMixer.

thanks for all the answers but im running through another error message when i run the program.

it goes as follow:

3
What Is The Color Of The Sky?
sky? the of color the is What
What is the color of the sky?
What is the color of the water?
Wh@t !$ the c0l0r 0f the $ky?
/home/ccuser/workspace/intermediate-javascript_messageMixer/messageMixer.js:30
  return word.split(" ").reverse().join(" ");
             ^

TypeError: Cannot read property 'split' of undefined
    at Object.reverseWord (messageMixer.js:26:10)
    at Object.palindrome (messageMixer.js:56:33)
    at Object.<anonymous> (message.js:16:26)
    at Module._compile (module.js:571:32)
    at loader (/home/ccuser/node_modules/babel-register/lib/node.js:158:5)
    at Object.require.extensions.(anonymous function) [as .js] (/home/ccuser/node_modules/babel-register/lib/node.js:168:7)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Function.Module.runMain (module.js:605:10)

here’s my code for messageMixer.js:

const MessageMixer = {};

MessageMixer.countCharacter = function countCharacter(inputString, inputCharacter) {
  let count = 0;
  let string = inputString.toLowerCase();
  let character = inputCharacter.toLowerCase();
    for (let i = 0; i < string.length; i++) {
      if (string[i] === character) {
         count++;
      }
    }
  return count; 
};

MessageMixer.capitalizeFirstCharacterOfWords = function capitalizeFirstCharacterOfWords(string) {
  let arr = string.split(" ");  
    for (let i = 0; i < arr.length; i++) {  
      let word = arr[i];
        arr[i] = word[0].toUpperCase() + word.substring(1); 
    }
  return arr.join(" "); 
};


MessageMixer.reverseWord = function reverseWord(word) {
  return word.split(" ").reverse().join(" ");
};

MessageMixer.reverseAllWords = function reverseAllWords(sentence) {
  let words = sentence.split(" ");
    for (let i = 0; i < words.length; i++) {
      words[i] = MessageMixer.reverseWord(words[i]);
    }
   return words.join(" ");
};


MessageMixer.replaceFirstOccurence = function replaceFirstOccurence(string, toBeReplaced, replaceWith) {
  return string.replace(toBeReplaced, replaceWith);
};


MessageMixer.replaceAllOccurrences = function replaceAllOccurrences(string, toBeReplaced, replaceWith) {
  return string.split(toBeReplaced).join(replaceWith);
};

MessageMixer.encode = function encode(string) {
  let replacementObject = { "a": "@", "s": "$", "i": "!", "o":"0" };
    for (let key in replacementObject) {
      string = MessageMixer.replaceAllOccurrences(string, key, replacementObject[key]); 
    }	
    return string;
};

MessageMixer.palindrome = function palindrome(str){
  return `${str} ${MessageMixer.reverseWord(str)}`
}

MessageMixer.pigLatin = function pigLatin(sentence, character){
  return sentence.split(' ').join(character + ' ')
}

export default MessageMixer;```

and for message.js:

//const MessageMixer = require('./messageMixer.js');

import MessageMixer from './messageMixer'

function displayMessage() {
  console.log(MessageMixer.countCharacter("What is the color of the sky?", "t"));
  console.log(MessageMixer.capitalizeFirstCharacterOfWords("What is the color of the sky?"));
  console.log(MessageMixer.reverseWord("What is the color of the sky?"));
  console.log(MessageMixer.reverseAllWords("What is the color of the sky?"));
  console.log(MessageMixer.replaceFirstOccurence("What is the color of the sky?", "sky", "water"));
  console.log(MessageMixer.encode("What is the color of the sky?"));
}

displayMessage();

console.log(MessageMixer.palindrome());

You are getting the error because your MessageMixer.palindrome() function call needs an argument. :wink:

Side note: I would consider creating a const message = 'What is the color of the sky?' as the first line in your displayMessage() function, and then using message as the argument instead of typing out the whole string for each function. Just a suggestion.

Hello. I really need help because I don’t know what to do. Every time I add module.export I get an error message.

/home/ccuser/workspace/intermediate-javascript_messageMixer/messageMixer.js:57
exports.default = MessageMixer;
^

ReferenceError: MessageMixer is not defined
at Object. (messageMixer.js:55:16)
at Module._compile (module.js:571:32)
at loader (/home/ccuser/node_modules/babel-register/lib/node.js:158:5)
at Object.require.extensions.(anonymous function) [as .js] (/home/ccuser/node_modules/babel-register/lib/node.js:168:7)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Function.Module.runMain (module.js:605:10)
at /home/ccuser/node_modules/babel-cli/lib/_babel-node.js:171:48
at Object. (/home/ccuser/node_modules/babel-cli/lib/_babel-node.js:172:7)

This is my code.

const MassageMixer = {};

MassageMixer.countCharacter = function(inputString, inputCharacter) {

  let count = 0;

  let string = inputString.toLowerCase();

  let character = inputCharacter.toLowerCase();

    for (let i = 0; i < string.length; i++) {

      if (string[i] === character) {

         count++;

      }

    }

  return count; 

};

MassageMixer.capitalizeFirstCharacterOfWords = function(string) {

  let arr = string.split(" ");  

    for (let i = 0; i < arr.length; i++) {  

      let word = arr[i];

        arr[i] = word[0].toUpperCase() + word.substring(1); 

    }

  return arr.join(" "); 

};

MassageMixer.reverseWord = function(word) {

  return word.split("").reverse().join("");

};

MassageMixer.reverseAllWords = function(sentence) {

  let words = sentence.split(" ");

    for (let i = 0; i < words.length; i++) {

      words[i] = MassageMixer.reverseWord(words[i]);

    }

   return words.join(" ");

};

MassageMixer.replaceFirstOccurence= function(string, toBeReplaced, replaceWith) {

  return string.replace(toBeReplaced, replaceWith);

};

MassageMixer.replaceAllOccurrences = function(string, toBeReplaced, replaceWith) {

  return string.split(toBeReplaced).join(replaceWith);

};

MassageMixer.encode = function(string) {

  let replacementObject = { "a": "@", "s": "$", "i": "!", "o":"0" };

    for (let key in replacementObject) {

      string = MassageMixer.replaceAllOccurrences(string, key, replacementObject[key]); 

    } 

    return string;

};

module.exports = MessageMixer;

Hello, @armeniainfo923436861, and welcome to the forums!

The error message is telling you exactly what the problem is.

Look at your code, and identify where MessageMixer was supposed to be defined. Where is the line that shows MessageMixer = something? Is there one? :wink:

Note: I formatted the code in your post. Please review How do I format code in my posts?

It shows error only when I add ‘export default’, before that everything is Ok. That is why I can’t define the problem.

Read your code. Don’t gloss over it because you know what you want it to say. Read what it actually says. If you can find it, paste the line of code in a reply where you defined MessageMixer.

You are trying to export something that doesn’t exist. If I did this:

const a = {};

module.exports = b;

I’d get the same error. It would say, ReferenceError: b is not defined