...

View Full Version : Creating a function



stallion150172
06-09-2004, 09:58 PM
I have created 2 functions, one to test if a charater is a vowel and the other remove a vowel.
I can't seem to get the dropVowel function to erhm 'Function propally'!!!
Please advise on what i am doing wrong?



function isVowel(aCharacter)
/************************************************************************/
/*Function takes one argument, a string which consists of a single */
/*character. The function returns the boleen value true if the character*/
/*is one of a, A, e, E, i, I, o, O, u, U. If the character is not one of*/
/*these the function returns the bolean value false. */
/************************************************************************/
{
return ((aCharacter == 'a') ||
(aCharacter == 'A') ||
(aCharacter == 'e') ||
(aCharacter == 'E') ||
(aCharacter == 'i') ||
(aCharacter == 'I') ||
(aCharacter == 'o') ||
(aCharacter == 'O') ||
(aCharacter == 'u') ||
(aCharacter == 'U'))
};

function dropVowels(aString)
/************************************************************************/
/*Function takes one argument, a string. */
/*Function returns a new string with vowels removed. */
/************************************************************************/
{

var resultString;

resultString = '';

for (var position = 0; position < aString.length; position = position + 1)
{
if (aString.charAt(position) ! isVowel(aCharacter))
{
resultString = aString.charAt(position)
}
};
return resultString
};

Thanks for your time and effort
STaLLiON

Vladdy
06-09-2004, 10:28 PM
Suggested reading:
http://devedge.netscape.com/library/manuals/2000/javascript/1.5/guide/regexp.html#1010922

- it will enable you to have a single line instead of a page you posted

stallion150172
06-09-2004, 11:50 PM
Don't really think this helped on the question asked,
replies on the given subject would be more usefull or am i just being thick and you have really helped me!!!

What i really need is to be able to create the function and understand how i did it so i thought i could get an answer and then reverse engineer the outcome and maybe, just maybe it might sink in :thumbsup:

Please help
Regards
STaLLiON

nolachrymose
06-10-2004, 12:02 AM
function dropVowels(s) {
return s.replace(/[aeiou]/gi, '');
}

Hope that helps!

Happy coding! :)

Oakendin
06-10-2004, 01:18 AM
Vladdy pointed you to a Regular Expressions (RegExp) lesson. nolachrymose's solution was the use of RegExp.

your two functions can look like this:


function isVowel(aCharacter) {
if(aCharacter.match(/[aeiou]/gi))
return true;
else
return false;
}

function dropVowels(s) {
return s.replace(/[aeiou]/gi, '');
}

Vladdy
06-10-2004, 01:33 AM
isVowel can be even shorter:


function isVowel(aCharacter)
{ return aCharacter.match(/[aeiou]/gi);
}

liorean
06-10-2004, 01:43 AM
And it could be better coded as:

String.prototype.isVowel=function(){
return /[aeiou]/gi.test(this);
// Y can also be a wovel (e.g. as used in pry, but not as used in yeast),
// as can W - but they can act as consonants as well.
// Any suggestions as to handling this?
};

nolachrymose
06-10-2004, 03:32 AM
I don't think there would be any simple way to do this, because "y" is be a vowel in "pry," but it's not in "lonely" and other adverbs (and other words, but adverbs just tend to end in "ly.")

Happy coding! :)

stallion150172
06-10-2004, 09:19 AM
Cheers Guys

This has really helped :D

Regards
STaLLiON

penelope_p
07-08-2004, 01:25 AM
So why you called Stallion....? ;)

Looked at your code - was wondering how you got on with it...?

Can you post the working version - I'm curious, just picking up this stuff myself - having similar problems....

Catch you soon.

Penelope.
x

jbot
07-08-2004, 11:58 AM
So why you called Stallion....? ;)

maybe he's been drinking Shrek's magic love potion :p





String.prototype.isVowel=function(){
return /[aeiou]/gi.test(this);
// Y can also be a wovel (e.g. as used in pry, but not as used in yeast),
// as can W - but they can act as consonants as well.
// Any suggestions as to handling this?
};



perhaps you could invoke an RPC on a dictionary using XmlHttpRequest, checking to see that certain dubious chars in the string are vowels or consonants? :cool:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum