Message Mixer step 8 error

Hi there. I am having issues where I am getting an error message at step 8 of the MessageMixer project but I don’t understand why. I have watched the video and my code seems to match it and I have seen a few responses to users with the seemingly same issue on the forum, but the none of the solutions are problems that my code has.

The full error message:

/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

The full messageMixer.js file:

const MessageMixer = {};

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


module.export = MessageMixer;

The full message.js file:

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

Some of the suggested solutions from other posts:

Apologies for making a new thread but those seem to be all the solutions on the forum and I am still stuck so I don’t think my issue is related.

Thanks in advance for any help! (this is my first post. )

Interestingly as I continued to work past step 8 to keep moving, it started to work but only because I changed the export and import methods it seems.

All that was changed was adding the palindrome() and pigLatin() functions (though they are not yet referenced in the displayObject() function) and altered the export.module syntax to export default and replaced using the require() syntax with the import keyword as instructed by the exercise.

Here is the new code for messageMixer.js:

const MessageMixer = {};

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

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

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

export default MessageMixer;

And the new code for message.js:

import MessageMixer from './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();
1 Like

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