Snippets

Coel May Letter functions

Created by Coel May
/* **********
 *  Letter
 * --------------------------------------------------
 *
 * PUBLIC DOMAIN.
 *
 */

var Letter = {
  vowels: [
    'a', 'e', 'i', 'o', 'u'
  ],

  consonants: [
    'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n',
    'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'
  ],

  isUpperCase: function (letter) {
    "use strict";
    return letter.toUpperCase() === letter;
  },

  isLowerCase: function (letter) {
    "use strict";
    return letter.toLowerCase() === letter;
  },

  isVowel: function (letter) {
    "use strict";
    var i = 0;
    for (i = 0; i < this.vowels.length; i += 1) {
      if (letter.toUpperCase() === this.vowels[i] ||
        letter.toLowerCase() === this.vowels[i]) {
        return true;
      }
    }
    return false;
  },

  isConsonant: function (letter) {
    "use strict";
    var i = 0;
    for (i = 0; i < this.consonants.length; i += 1) {
      if (letter.toUpperCase() === this.consonants[i] ||
        letter.toLowerCase() === this.consonants[i]) {
        return true;
      }
    }
    return false;
  },

  stringToArray: function (str) {
    "use strict";
    var arr = [],
        a = 0;
    for (a = 0; a < str.length; a += 1) {
      arr[a] = str.charAt(a);
    }
    return arr;
  },

  removeVowels: function (str) {
    "use strict";
    var i = 0,
        strArr = str.split(''),
        newStr = "";
    for (i = 0; i < str.length; i += 1) {
      if (!this.isVowel(strArr[i])) {
        newStr += strArr[i];
      }
    }
    return newStr;
  },

  removeConsonants: function (str) {
    "use strict";
    var c = 0,
        strArr = str.split(''),
        newStr = "";
    for (c = 0; c < strArr.length; c += 1) {
      if (!this.isConsonant(strArr[c])) {
        newStr += strArr[c];
      }
    }
    return newStr;
  },

  swapLetters: function (str, originalArr, newArr) {
    "use strict";
    var strArr = str.split(''),
        x,  // counter
        letterPosition;
    if (Array.isArray(originalArr) &&
        Array.isArray(newArr) &&
        originalArr.length === newArr.length) {
      for (x = 0; x < strArr.length; x += 1) {
        if (this.isVowel(strArr[x])) {
          letterPosition = originalArr.indexOf(strArr[x]);
          strArr[x] = newArr[letterPosition];
        }
      }
      return strArr.join('');
    }
    return null;
  }
};

Comments (0)