Message Mixer 'Cannot set property'

Hey, I have been doing the Message Mixer projects and I got stuck on step 8 which should output the functions when I run the program in message.js. I receive “TypeError: cannot set property ‘countCharacter’ of undefined”, even though everything seems fine. Does anyone else see the issue with my code?

messageMixer.js

MessageMixer.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; 
};

MessageMixer.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(" "); 
};


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

MessageMixer.reverseAllWords = function (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 (string, toBeReplaced, replaceWith) {
  return string.replace(toBeReplaced, replaceWith);
};


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

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

displayMessage();

const MessageMixer = {};

module.exports = MessageMixer;

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();
const MessageMixer = {};

module.exports = MessageMixer;

I think the issue is that you are declaring the MessageMixer empty object on the second last line of code after trying to add all the functions to it. I would create this object at the very top of the file and include all the methods inside of the object. Then use module.exports like you are doing on the last line.

1 Like

Thank you so much for your answer. Can’t believe I wasted so much time and didn’t realize this was the issue.

2 Likes