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)

i did an aditional pigLatin function named pigLatin1 that utilizes the form of pig latin i learned where you move the first letter of the word to the end and add ‘ay’ to the end as well.

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


function switch1 (word) {
  let let1 = '';
  let arr = word.split('');
  for (let x of arr) {
    if (arr[0] === x) {
      let1 = x;
    }
  }
  let newArr = arr.slice(1);
  newArr.push(let1);
  arr = newArr;
  word = arr.join('').toLowerCase()
  word = word + 'ay';
  return word
};


function pigLatin1 (sentence) {
  let arrR = sentence.split('');
  let last = arrR[arrR.length - 1];
  if (last === '.' || last === '?' || last === '!') {
    arrR.pop();
  } else {
    last = '.';
  }
  sentence = arrR.join('');
  let arr = sentence.split(' ');
  let newArr = [];
  for (let word of arr) {
    newArr.push(switch1(word));
  }
  sentence = newArr.join(' ');
  let newSen = sentence.split(' ');
  let word = newSen[0];
  let end = newSen[newSen.length - 1];
  let arr2 = end.split('');
  arr2.push(last);
  end = arr2.join('');
  newSen[0] = word[0].toUpperCase() + word.substring(1);
  newSen[newSen.length - 1] = end
  sentence = newSen.join(' ');
  return sentence;
};

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

and the message is:

import { countCharacter, capitalizeFirstCharacterOfWords, reverseWord, reverseAllWords, replaceFirstOccurence, replaceAllOccurrences, encode, palindrome, pigLatin, pigLatin1 } 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(palindrome("What is the color of the sky?"));
  console.log(pigLatin("What is the color of the sky?", 'o'));
  console.log(pigLatin1("What is the color of the sky?"))
}

when you get to question 14 we start using the import keyword and export by naming the variable which means we need to delete the MessageMixer module, therefor we no longer use methods and reformat them into functions.

you can delete the two MessageMixer objects at the top, those are no longer needed. as well as the export default MessageMixer at the bottom.