-1

My application requirement is if a single word has a vowel like "anupam" then after its last vowel add "xy". So its result should be "anupxy". But if two vowel continue with in a word then it should be like :

      Oa = Oaxy
    and au = auxy
Gardenhire = Gardxy

Parker = Parkxy

Arney = Arnxy

There are 4 rules.
1. The name cuts off at the second vowel and is replaced by 'xy.'
2. connected vowels count as a single and should stay together.
3. EXCEPTION: when the last letter is 'x' another 'x' is not added.
4. Names with only two connected vowels should have "xy" added to the end

I don't know where is my mistake, My code is :

function doit(userName) {
        var temp = userName.toLowerCase();
        var vowels = "aeiouy"
        var count = 0;

        if(userName) {
            for(var i=0; i<temp.length; i++) {

                if( vowels.indexOf(temp.charAt(i)) > -1 ) {
                    count++;
                    if(count==1) {
                        while( vowels.indexOf(temp.charAt(++i)) != -1 );
                        i--;
                    } else
                        break;
                }
            }

            userName = userName.substr(0, i);
            if( userName.charAt(userName.length-1) == 's' )
                userName += "y";
            else
                userName += "sy";
        } else
            userName = 'Take a lap, Dummy';
        return userName.toUpperCase();

    }
Anup
  • 3,283
  • 1
  • 28
  • 37

4 Answers4

2

Regular expression is the way to go.

var word = "anumap";
var transformed = word.replace(/(\w+[aeiou]+).*/i, " $1xy");

I've created an interactive fiddle: http://jsfiddle.net/53qH6

The "\w+" means match any word character. This will get all letters before the last vowel. The [] and vowels in them are what we are looking for and the + outside of the brackets means that there must be at least one vowel. The ".*" means match anything that comes next (anything after the last vowel) The parenthesis means capture it into a variable ($1).

Jody
  • 1,543
  • 11
  • 20
  • +1. Nice regex, but in practice regex may be a bad choice as it's hard to read and maintain. This is an example where a comment indicating the purpose and logic of the regex absolutely must accompany the code. – Zack Burt Apr 09 '14 at 03:53
  • You're right @ZacharyBurt, I am writing on an ipad which has proven to be a terrible idea, so I got lazy. I added an explanation. If anything doesn't make sense please ask. I would argue however that this relatively simple regex is much easier to read and maintain than the doit() function above ;) – Jody Apr 09 '14 at 04:05
1

I recommend you use a regular expression, particularly when using an interpreted language. Much simpler code, and likely much better performance as well. See:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

http://www.regular-expressions.info/javascript.html

Shawn C
  • 356
  • 2
  • 5
1

like this:

function doit(userName) {
    var str = userName || "";
    var vowels = ["a","e","i","o","u"];
    var suffix = "xy";

    var str_arr = str.split("");
    var str_arr_rev = str_arr.reverse();

    $.each(str_arr_rev, function (i, item) {
        if ($.inArray(item.toLowerCase(), vowels) > -1) {
            last_vowel_index = i;
            return false;
        }
    });

    if (last_vowel_index == -1) {
        $.each(str_arr_rev, function (i, item) {
            if (item.toLowerCase() == "y") {
                last_vowel_index = i;
                return false;
            }
        });
    }

    if (last_vowel_index > -1)
        str_arr_rev[last_vowel_index] = str_arr_rev[last_vowel_index] + suffix;

    return str_arr_rev.reverse().join("");
}
Greg
  • 8,574
  • 21
  • 67
  • 109
1

Not certain about the second portion of requirement,

But if two vowel continue with in a word then it should be like :

Oa = Oaxy and au = auxy -Anup

Try this pattern

html

   <input type="text" value="" /><br />
   <div id="name"></div>

js

Edit

original piece did not convert "anupam" to "ANUPAXY", regexes may still use adjusting

$(function () {
    $("input").on("change", function(e) {       
    $("#name")
        .html(function (index, o) {
        var v = /[aeiou]+.$/gi;
        var o = $(e.target).val();
        var n = v.test(o);
        return (n ? String(o.replace(/[^aeiou]$/gi, "") + "xy").toUpperCase() : o)
    });
   });
})

jsfiddle http://jsfiddle.net/guest271314/nB9Qc/

See also

Is this the shortest javascript regex to find all uppercase consonants?

How to negate specific word in regex?

Break string after specific word and put remains on new line (Regex)

Community
  • 1
  • 1
guest271314
  • 1
  • 15
  • 104
  • 177