Message Mixer project

https://www.codecademy.com/paths/web-development/tracks/webdev-intermediate-javascript/modules/intermediate-javascript-modules/projects/message-mixer

I’ve just started this project and while I’m not exactly stuck I’d like to understand something.

I’m making all the separate functions in messageMixer.js into properties of an object. Why can’t they be inside the object, like this:

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

},

Instead of leaving the object empty? The hint and the video show it like this:

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

My method gets errors so I’m clearly wrong but I’d like to understand the difference between this situation and others where we’ve added functions inside objects.

when working with objects, we don’t use functions. We use properties and methods

the syntax is different for methods:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions

OK. I’m guilty of mixing up my syntax. But now I’ve straightened that out, is there any reason why I shouldn’t complete step 4 of the exercise like this, with each property inside the object?

const MessageMixer = {
      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; 
    },



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


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

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


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


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

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

    };

step 4 looks complete now :slight_smile:

Thanks. It’s not the same as the video/hint so always helpful to know whether what I think I’ve learned is correct :slight_smile: