Nodejs String Starts With beginsWithVowel()

Here you can find the source of beginsWithVowel()

Method Source Code

String.prototype.beginsWithVowel = function(){
   if (this == "") return false;
   return "aeiou".split("").indexOf(this[0].toLowerCase()) !== -1
}

function translatePigLatin(str) {
   var nonv = str.split(/[aeiou]/)[0].length
   if (nonv == 0){
       return str + "way";
    }else {//www. j a  v  a 2 s.  com
      return str.slice(nonv) + str.slice(0, nonv) + "ay";
    }
}

console.log(translatePigLatin("california")); // "aliforniacay".
console.log(translatePigLatin("paragraphs")) ; //"aragraphspay".
console.log(translatePigLatin("glove") ); // "oveglay".
console.log(translatePigLatin("algorithm")); // "algorithmway".
console.log(translatePigLatin("eight")); //"eightway".translatePigLatin("stragiht"));
console.log(translatePigLatin(""));
console.log(translatePigLatin("a"));
console.log(translatePigLatin("s"));

Related

  1. beginsWith(s)
    String.prototype.beginsWith = function(s){
        return this.indexOf(s) == 0;
    };
    
  2. beginsWith(s)
    String.prototype.beginsWith= function(s)
        return s===this.substring(0, s.length);
    if (!String.prototype.localeCompare)
        String.prototype.localeCompare = function(other)
            if (this < other)
                return -1;
    ...
    
  3. beginsWith(t)
    String.prototype.beginsWith = function(t) {
        'use strict';
        t = t.toString();
        return (t.toString() === this.substring(0, t.length));
    };
    String.prototype.trim = function() {
        'use strict';
        return this.replace(/^\s+/,'').replace(/\s+$/,'');
    };
    ...
    
  4. beginsWith(t)
    String.prototype.beginsWith = function(t) {
        'use strict';
        t = t.toString();
        return (t.toString() === this.substring(0, t.length));
    };
    
  5. beginsWith(t, i)
    String.prototype.beginsWith = function(t, i) {
      if (i == false) {
        return (t == this.substring(0, t.length));
      } else {
        return (t.toLowerCase() == this.substring(0, t.length).toLowerCase());
    };