Message Mixer

Hello buddies, i need ur help in resolving problem in this exercise https://www.codecademy.com/courses/introduction-to-javascript/projects/message-mixer

This is my ERROR

/home/ccuser/workspace/intermediate-javascript_messageMixer/messageMixer.js:66
exports.countCharacter = countCharacter;

                         ^

ReferenceError: countCharacter is not defined
    at Object.<anonymous> (messageMixer.js:66:10)
    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.<anonymous> (/home/ccuser/node_modules/babel-cli/lib/_babel-node.js:172:7)

This is my code in the MeassageMixer.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("");
};

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

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


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

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

export default MessageMixer;

export { countCharacter, capitalizeFirstCharacterOfWords, reverseWord, reverseAllWords, replaceFirstOccurence, replaceAllOccurrences, encode, palindrome, pigLatin };

This is my code in Message.js

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

import { countCharacter, capitalizeFirstCharacterOfWords, reverseWord, reverseAllWords, replaceFirstOccurence, replaceAllOccurrences, encode, palindrome, pigLatin} from './messageMixer';

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

displayMessage();

countCharacter is a property of MessageMixer, so it should be MessageMixer.countCharacter, but you already export MessageMixer, so it seems duplicate

properties of an object are not available in the global namespace.

i would need to see the lesson, but the url you posted doesn’t work for me

https://www.codecademy.com/courses/introduction-to-javascript/projects/message-mixer

this is confusing, seems you just need to write functions:

function countCharacter(inputString, inputCharacter) 

this doesn’t make much sense. I like your approach better .

i don’t know what to do

you made methods, you should use functions.

i’ve to do methods by codeacademy task

i will ask around by the other mods, this doesn’t make much sense

i would recommend only exporting MessageMixer object, then in messages.js, import MessageMixer object and update all the function calls to methods calls

2 Likes
const MessageMixer = {};

const MessageMixer = {};

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

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


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

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


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


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

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


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

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

export default MessageMixer;

export { countCharacter, capitalizeFirstCharacterOfWords, reverseWord, reverseAllWords, replaceFirstOccurence, replaceAllOccurrences, encode, palindrome, pigLatin };



new fault

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

ReferenceError: countCharacter is not defined
    at Object.<anonymous> (messageMixer.js:3: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
    at Object.<anonymous> (/home/ccuser/node_modules/babel-cli/lib/_babel-node.js:172:7)

that’s all , thanks, it works)