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:
- Be cognizant of capitalization. So I made sure to copy all instances of MessageMixer. to insure that I didn’t have a capitalization typo
- Don’t declare MessageMixer object at the bottom of the messageMixer.js file. I double checked and that is not the issue, I have declared that first thing in my code. (here is another example of that being the issue)
- Don’t try to import your module twice. Was not the issue for me
- This thread lost me, it looks like the suggestion was to use functions instead of methods and they went completely off script. I could try that, but I would like to find out what is wrong with the approach suggested in the lesson and solution video
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. )