There is an error in step 13 of project Message Mixer when I run my code, do not know what is wrong. Thanks

in the step 13, I run the code, there is an undefined showed on the screen in the result, can not find what is wrong, seems everything is right. The link is here messageMixer

the messagerMixer.js code is paste below:

let 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.pigLatin = function(sentence, character){
sentence.split(’ ').join(character + ’ ');
};

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

export default MessageMixer;

the message.js code is here:

import MessageMixer from ‘./messageMixer’;

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?”));
console.log(MessageMixer.pigLatin(‘What is the color of the sky?’, 'y '));
console.log(MessageMixer.palindrome(“What is the color of the sky?”));
}

displayMessage();

The result is there, there is an undefined, quite unsure why

3
What Is The Color Of The Sky?
?yks eht fo roloc eht si tahW
tahW si eht roloc fo eht ?yks
What is the color of the water?
Wh@t !$ the c0l0r 0f the $ky?
undefined
What is the color of the sky? ?yks eht fo roloc eht si tahW

The first thing you should do is isolate which one is logging undefined so you can start debugging. Usually this would involve commenting things out and/or setting up more logging, but in this case you can count your lines of output and also compare the output to what you expect each function should produce.

console.log(MessageMixer.pigLatin('What is the color of the sky?', 'y '));

so you need to examine your pigLatin function in your messagerMixer.js. There is something that function is missing that all the other ones that are working have.

1 Like

Thank you very much, according to your guide, I find the reason is the lack of ‘return’ when I define the function.

2 Likes